Regex for re.match not working as expected.

Reply

Join Date: Oct 2008
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.

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Oct 2008
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.

 
0
  #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 Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 263
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

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

 
0
  #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 Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

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

 
0
  #5
Oct 10th, 2008
If the file name is a variable returned from some source:
  1. >>> s = "c:\test.doc"
  2. >>> s1 = repr(s).strip("'")
  3. >>> s1
  4. 'c:\\test.doc'
  5. >>>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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