| | |
Regex for re.match not working as expected.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
Tag cloud for Python
advanced aliased bash beginner bits calling casino changecolor class clear code command convert corners count csv cturtle cursor definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output panel parsing path port prime programming projects py py-mailer py2exe pygame pyqt python random rational raw_input recursion return scrolledtext signal singleton stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop windows word wxpython






