python 正则表达式将 \t 字面解释为 \t 而不是制表符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1340162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:00:21  来源:igfitidea点击:

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

pythonregex

提问by Nadia Alramli

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

我正在尝试匹配带有反斜杠的文本序列,例如 Windows 路径。

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.

现在,当我在 python 中与 regexp 匹配时,它得到匹配,但模块将所有反斜杠后跟有效的转义字符(即t)解释为转义序列,这不是我想要的。

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!

编辑:好吧,我错过了与包含反斜杠的文本匹配的正则表达式是 (.*)。我已经尝试过原始符号(例如在 awnsers 中),但它对我的情况没有帮助。或者我做错了。EDIT2:做错了。谢谢男孩/女孩!

回答by Nadia Alramli

Use double backslashes with r like this

像这样在 r 中使用双反斜杠

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

From python docs:

来自 python文档

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:

当想要匹配文字反斜杠时,必须在正则表达式中对其进行转义。使用原始字符串表示法,这意味着 r"\"。如果没有原始字符串表示法,则必须使用“\\”,使以下代码行在功能上相同:

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

回答by Daniel Roseman

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

r定义正则表达式时始终使用前缀。这将告诉 Python 将字符串视为原始字符串,因此它不会执行任何标准处理。

 regex = r'\t'