| | |
Regex for re.match not working as expected.
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
I have the following code:
the problem I am having is that this is always evaluating to false.
python Syntax (Toggle Plain Text)
import re string = 'c:\test.doc' if re.match(r":\\[0-9a-z]", string): ##r":\\[0-9a-z]" should match ':\t' print 'true' else: print 'false'
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
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)
import re s = r'c:\test.doc' m = re.search(r":(\\[0-9a-z])", s) if m: print m.group(1) else: print m
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
If the file name is a variable returned from some source:
Python Syntax (Toggle Plain Text)
>>> s = "c:\test.doc" >>> s1 = repr(s).strip("'") >>> s1 'c:\\test.doc' >>>
![]() |
Other Threads in the Python Forum
- Previous Thread: Up for a challenge? Complete coding noob in need of help to code on mobile platform
- Next Thread: win32clipboard problem
| Thread Tools | Search this Thread |
abrupt accessdenied ansi anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






