python - Python 正则表达式中的反斜杠字符

标签 python regex

Python documentation for Regex ,作者提到:

regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This conflicts with Python’s usage of the same character for the same purpose in string literals.

他接着给出了一个匹配 \section 的例子在正则表达式中:

to match a literal backslash, one has to write '\\' as the RE string, because the regular expression must be \, and each backslash must be expressed as \ inside a regular Python string literal. In REs that feature backslashes repeatedly, this leads to lots of repeated backslashes and makes the resulting strings difficult to understand.

然后他说解决这个“反斜杠瘟疫”的方法是以 r 开始字符串。将其转换为原始字符串。

后来,他给出了使用正则表达式的示例:

p = re.compile('\d+')
p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping')

结果是:

['12', '11', '10']

我很困惑为什么我们不需要包含 r在这种情况下之前 '\d+' 。我认为,根据之前对反斜杠的解释,我们需要告诉 Python 该字符串中的反斜杠不是它所知道的反斜杠。

最佳答案

Python 只识别一些以 \ 开头的序列作为转义序列。例如,\d 不是已知的转义序列,因此对于这种特殊情况,无需转义反斜杠以将其保留在那里。

(在 Python 3.6 中)"\d""\\d" 是等效的:

>>> "\d" == "\\d"
True
>>> r"\d" == "\\d"
True

以下是所有已识别的转义序列的列表:https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

关于python - Python 正则表达式中的反斜杠字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61144812/

相关文章:

python - 是否可以在 Python 中模拟 Scala 的特性?

regex - 在字符串中向后查找属于集合的第一个子字符串(正则表达式?)

regex - 我们如何将正则表达式转换为 ANTLR 4 表达式

ruby - 从字符串中去除括号的结尾集

java - 如何在 Java 中将字符串子串到第二个点 (.)?

java - 除了 RPC 调用之外,还有什么原因会导致我的 App Engine 程序花费这么长时间

python - 使用ast.literal_eval()清理数据时出现语法错误

Python-跳过有错误的行并处理下一行

python - 传递带有 "implicit"参数的函数

java - 匹配器结果 - 正则表达式