RSS Forums RSS

Regex for re.match not working as expected.

Please support our Python advertiser: Programming Forums
Reply
Posts: 2
Reputation: unkwntech is an unknown quantity at this point 
Solved Threads: 0
unkwntech unkwntech is offline Offline
Newbie Poster

Regex for re.match not working as expected.

  #1  
Oct 9th, 2008
I have the following code:
  1. import re
  2. string = 'c:\test.doc'
  3. if re.match(r":\\[0-9a-z]", string):
  4. ##r":\\[0-9a-z]" should match ':\t'
  5. print 'true'
  6. else:
  7. print 'false'
the problem I am having is that this is always evaluating to false.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 109
Reputation: solsteel is on a distinguished road 
Solved Threads: 26
solsteel solsteel is offline Offline
Junior Poster

Re: Regex for re.match not working as expected.

  #2  
Oct 9th, 2008
The substring "\t" in string is evaluated as a tab character. The re method match only matches at the beginning of a string. Try this:
  1. import re
  2. s = r'c:\test.doc'
  3. m = re.search(r":(\\[0-9a-z])", s)
  4. if m:
  5. print m.group(1)
  6. else:
  7. print m
Reply With Quote  
Posts: 2
Reputation: unkwntech is an unknown quantity at this point 
Solved Threads: 0
unkwntech unkwntech is offline Offline
Newbie Poster

Re: Regex for re.match not working as expected.

  #3  
Oct 9th, 2008
Ok, I'm not used to that yet, in PHP if I encased the string in ' it would have not evaluated the \t, or any other special character.

Thanks.
Reply With Quote  
Posts: 770
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 139
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Regex for re.match not working as expected.

  #4  
Oct 10th, 2008
you could just as easily do s = 'c:\\test.doc'
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote  
Posts: 109
Reputation: solsteel is on a distinguished road 
Solved Threads: 26
solsteel solsteel is offline Offline
Junior Poster

Re: Regex for re.match not working as expected.

  #5  
Oct 10th, 2008
If the file name is a variable returned from some source:
>>> s = "c:\test.doc"
>>> s1 = repr(s).strip("'")
>>> s1
'c:\\test.doc'
>>> 
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 626 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:24 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC