Regular Exp

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Regular Exp

 
0
  #1
Jun 24th, 2005
Sorry, I know that this is probably the wrong section to post this question in, but I'm sort of confused as to which one would have been appropriate. Anyways, I've been using regular expressions to find specific information such as e-mail addresses and whatnot inside textfiles, but I can't get my head over this one problem.

The problem my expression faces, is getting it to stop looking at a string. I was wondering if there's anyway I could expand the NOT parameters to include entire strings, rather then just one character.

For example, given the string:

Only pick up strings without Capital letters!

My regular expression would get:

Only pick up strings without Capital letters!

Any help would be great
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Regular Exp

 
0
  #2
Jun 25th, 2005
I wrote this little function in Python. It scans a string and returns True the moment it finds a capital letter, otherwise False. Hope this helps ...
  1. # tested with Python24
  2.  
  3. def hasCap(s):
  4. """returns True if the string s contains a capital letter"""
  5. for num in range(65, 91): # A to Z
  6. capLetter = chr(num)
  7. if capLetter in s:
  8. return True
  9. return False
  10.  
  11. str1 = 'Only pick up strings without Capital letters!'
  12. str2 = 'only pick up strings without capital letters!'
  13.  
  14. # test the function hasCap()
  15. if hasCap(str1):
  16. print "str1 has a capital letter"
  17. else:
  18. print "str1 has no capital letter"
  19.  
  20. if hasCap(str2):
  21. print "str2 has a capital letter"
  22. else:
  23. print "str2 has no capital letter"
Last edited by vegaseat; Oct 25th, 2009 at 2:25 pm. Reason: update code tags
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Re: Regular Exp

 
0
  #3
Jun 25th, 2005
Thanks, that's exactly what I want it to do, except I'm trying to do that using Regular Expressions. Is there a way I could change the anchors (^ and $) so that it reads the beginning of every string, rather then at the beginning of every file?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Regular Exp

 
0
  #4
Jun 26th, 2005
I stopped at this regex for Python site:
http://www.amk.ca/python/howto/regex/
to refresh my brain on regex. I am really rusty!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Re: Regular Exp

 
0
  #5
Jun 29th, 2005
Thanks for your help. :mrgreen:

It took me quite a while, but I found out how to do it. Just in case anyone else has similar problems, what I did was I made an or statement that said the first letter had to be a space, followed by a capital, or a capital at the start of a string.
Last edited by Rete; Jun 29th, 2005 at 8:53 pm. Reason: typo
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC