| | |
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 |
alarm anydbm app beginner cipher cmd conversion coordinates corners curves cx-freeze data decimals definedlines development dictionary directory dynamic error excel feet file float format function generator getvalue halp handling homework http import input ip itunes keycontrol leftmouse line linux list lists loan loop maintain maze millimeter module mouse number numbers output parsing path prime programming push py2exe pygame pymailer python queue random rational raw_input recursion recursive schedule screensaverloopinactive script scrolledtext searchingfile slicenotation split sqlite ssh string strings sudokusolver text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wxpython xlwt






