| | |
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 alarm ansi anti apache approximation array assignment backend beginner binary bluetooth builtin calculator character cmd converter countpasswordentry curved customdialog cx-freeze dan08 data decimals dictionary directory dynamic exe file float format function gnu graphics heads homework http ideas inches input java leftmouse library line lines linux list lists loop module mouse mysqlquery number numbers numeric output parsing path phonebook pointer prime programming progressbar push py2exe pygame python random recursion redirect schedule screensaverloopinactive script scrolledtext software sqlite statictext statistics string strings sudokusolver terminal text thread time tlapse tuple twoup ubuntu unicode urllib urllib2 variable ventrilo webservice wikipedia wordgame write wxpython xlib






