| | |
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
Views: 893 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm aliased application beginner calculator casino character code command cursor cx-freeze definedlines development dictionary dynamic error event examples excel exe file filename float format ftp function google graphics gui homework ideas import input java launcher line linux list lists logging loop matching microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook port prime program programming projects py2exe pygame pygtk pyqt pysimplewizard python random recursion recursive refresh return reverse scrolledtext shebang simple skinning sprite ssh statistics string strings table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip windows wxpython






