943,717 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1347
  • Python RSS
Oct 9th, 2008
0

Regex for re.match not working as expected.

Expand Post »
I have the following code:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
unkwntech is offline Offline
2 posts
since Oct 2008
Oct 9th, 2008
0

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

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:
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Oct 9th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
unkwntech is offline Offline
2 posts
since Oct 2008
Oct 10th, 2008
0

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

you could just as easily do s = 'c:\\test.doc'
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 10th, 2008
0

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

If the file name is a variable returned from some source:
Python Syntax (Toggle Plain Text)
  1. >>> s = "c:\test.doc"
  2. >>> s1 = repr(s).strip("'")
  3. >>> s1
  4. 'c:\\test.doc'
  5. >>>
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Up for a challenge? Complete coding noob in need of help to code on mobile platform
Next Thread in Python Forum Timeline: win32clipboard problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC