在re这一模块的官方文档的解释如下
Regular expressions use the backslash character ('') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals。
官方文档在解释之后给的例子:
So r"\n" is a two-character string containing '' and 'n', while "\n" is a one-character string containing a newline
所以如果正则表达式中用到类似'\d'(正则表达式中表示0-9这10个数字)这样的特殊形式就用raw string,因为raw string相较于普通string没有转义字符的概念只会把'\d'当成'\d',而不会有如下情况:
标签:string,re,Python,character,raw,special From: https://www.cnblogs.com/Yuetao-Meng/p/17027927.htmls = '\d'
s
'\d'
print(s)
\d
repr(s)
"'\\d'"