I have the following code:

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'

the problem I am having is that this is always evaluating to false.

Recommended Answers

All 4 Replies

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:

import re
s = r'c:\test.doc'
m = re.search(r":(\\[0-9a-z])", s)
if m:
      print m.group(1)
else:
      print m

Ok, I'm not used to that yet, in PHP if I encased the string in ' it would have not evaluated the \t, or any other special character.

Thanks.

you could just as easily do s = 'c:\\test.doc'

If the file name is a variable returned from some source:

>>> s = "c:\test.doc"
>>> s1 = repr(s).strip("'")
>>> s1
'c:\\test.doc'
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.