max
max

Reputation:

Regexp to literally interpret \t as \t and not tab

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!

Upvotes: 4

Views: 21485

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599856

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'

Upvotes: 1

Nadia Alramli
Nadia Alramli

Reputation: 114981

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 ...>

Upvotes: 11

Related Questions