tags:

views:

126

answers:

2

Hi,

I'm trying to match a sequence of text with backslashed in it, like a windows path.

Now, when I match with regexp in python, it gets the match, but the module interprets all backslashes followed by a valid escape char (i.e. t) as an escape sequence, which is not what I want.

How do I get it not to do that?

Thanks /m

EDIT: well, i missed that the regexp that matches the text that contains the backslash is a (.*). I've tried the raw notation (examplefied in the awnsers), but it does not help in my situation. Or im doing it wrong. EDIT2: Did it wrong. Thanks guys/girls!

+8  A: 

Use double backslashes with r like this

>>> re.match(r"\\t", r"\t")
<_sre.SRE_Match object at 0xb7ce5d78>

From python docs:

When one wants to match a literal backslash, it must be escaped in the regular expression. With raw string notation, this means r"\". Without raw string notation, one must use "\\", making the following lines of code functionally identical:

>>> re.match(r"\\", r"\\")
<_sre.SRE_Match object at ...>
>>> re.match("\\\\", r"\\")
<_sre.SRE_Match object at ...>
Nadia Alramli
it's a single double-backslash :)
SilentGhost
well, i missed that the regexp that matches the text that contains the backslash is a (.*). I've tried the raw notation above, but it does not help in my situation. Or im doing it wrong.
Can you update your question with an example and your actual regexp?
Nadia Alramli
A: 

Always use the r prefix when defining your regex. This will tell Python to treat the string as raw, so it doesn't do any of the standard processing.

 regex = r'\t'
Daniel Roseman