Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式-r前缀_Python_Regex_String_Literals_Prefix - Fatal编程技术网

Python正则表达式-r前缀

Python正则表达式-r前缀,python,regex,string,literals,prefix,Python,Regex,String,Literals,Prefix,如果不使用r前缀,有人能解释一下下面的示例1为什么起作用吗? 我认为无论何时使用转义序列,都必须使用r前缀。 示例2和示例3演示了这一点 # example 1 import re print (re.sub('\s+', ' ', 'hello there there')) # prints 'hello there there' - not expected as r prefix is not used # example 2 import re print (re.s

如果不使用
r
前缀,有人能解释一下下面的示例1为什么起作用吗? 我认为无论何时使用转义序列,都必须使用
r
前缀。 示例2和示例3演示了这一点

# example 1
import re
print (re.sub('\s+', ' ', 'hello     there      there'))
# prints 'hello there there' - not expected as r prefix is not used

# example 2
import re
print (re.sub(r'(\b\w+)(\s+\1\b)+', r'\1', 'hello     there      there'))
# prints 'hello     there' - as expected as r prefix is used

# example 3
import re
print (re.sub('(\b\w+)(\s+\1\b)+', '\1', 'hello     there      there'))
# prints 'hello     there      there' - as expected as r prefix is not used

因为
\
只有在转义序列有效时才开始转义序列

>>> '\n'
'\n'
>>> r'\n'
'\\n'
>>> print '\n'


>>> print r'\n'
\n
>>> '\s'
'\\s'
>>> r'\s'
'\\s'
>>> print '\s'
\s
>>> print r'\s'
\s
存在“r”或“r”前缀,在字符串中根据与标准C使用的规则类似的规则进行解释。识别的转义序列为:

Escape Sequence   Meaning Notes
\newline  Ignored  
\\    Backslash (\)    
\'    Single quote (')     
\"    Double quote (")     
\a    ASCII Bell (BEL)     
\b    ASCII Backspace (BS)     
\f    ASCII Formfeed (FF)  
\n    ASCII Linefeed (LF)  
\N{name}  Character named name in the Unicode database (Unicode only)  
\r    ASCII Carriage Return (CR)   
\t    ASCII Horizontal Tab (TAB)   
\uxxxx    Character with 16-bit hex value xxxx (Unicode only) 
\Uxxxxxxxx    Character with 32-bit hex value xxxxxxxx (Unicode only) 
\v    ASCII Vertical Tab (VT)  
\ooo  Character with octal value ooo
\xhh  Character with hex value hh
永远不要依赖原始字符串作为路径文本,因为原始字符串有一些非常特殊的内部工作机制,众所周知,这些机制会让人讨厌:

当出现“r”或“r”前缀时,反斜杠后面的字符将包含在字符串中而不作更改,并且所有反斜杠都保留在字符串中。例如,字符串literal
r“\n”
由两个字符组成:反斜杠和小写“n”。字符串引号可以用反斜杠转义,但反斜杠仍保留在字符串中;例如,
r“\”
是由两个字符组成的有效字符串文字;
r“\”
不是有效的字符串文字(即使是原始字符串也不能以奇数个反斜杠结尾)。具体来说,原始字符串不能以单个反斜杠结尾(因为反斜杠将转义下面的引号字符)。另外,请注意,后跟换行符的单个反斜杠将被解释为作为字符串一部分的这两个字符,而不是作为行继续符

为了更好地说明最后一点:

>>> r'\'
SyntaxError: EOL while scanning string literal
>>> r'\''
"\\'"
>>> '\'
SyntaxError: EOL while scanning string literal
>>> '\''
"'"
>>> 
>>> r'\\'
'\\\\'
>>> '\\'
'\\'
>>> print r'\\'
\\
>>> print r'\'
SyntaxError: EOL while scanning string literal
>>> print '\\'
\

并非所有涉及反斜杠的序列都是转义序列。
\t
\f
是转义序列,但
\s
不是。在非原始字符串文字中,任何不属于转义序列的
\
都被视为另一个
\

>>> "\s"
'\\s'
>>> "\t"
'\t'

<代码> \b/COD>是一个转义序列,但是,示例3失败了。(是的,有些人认为这个行为相当不幸)

表示R是一个“原始字符串”,即,反斜杠字符被逐字处理,而不是表示以下字符的特殊处理。

因此,
“\n”
是一个换行符
r'\n'
是两个字符-一个反斜杠和一个字母'n'
另一种写入方法是
'\\n'
,因为第一个反斜杠转义第二个反斜杠

写这篇文章的等效方法

print (re.sub(r'(\b\w+)(\s+\1\b)+', r'\1', 'hello     there      there'))

由于Python处理无效转义字符的方式,并非所有这些双反斜杠都是必需的-例如
'\s'='\\s'
,但是
'\b'
'\\b'
的情况并非如此。我的首选是显式的,并将所有反斜杠加倍。

尝试:

a = '\''
'
a = r'\''
\'
a = "\'"
'
a = r"\'"
\'
检查以下示例:

print r"123\n123" 
#outputs>>>
123\n123


print "123\n123"
#outputs>>>
123
123

没错。尽管@JT,我建议使用'\\s'或r'\s',否则您可能会无意中碰到一些转义序列,而不是您想要的。事实上:当您希望字符串包含反斜杠时,请始终使用原始字符串文字(而不是实际需要转义序列)@托马斯:
r
仍然会在某些序列出现在字符串末尾时转义:
r“\”
无效,为此必须执行
“\\\”
。如果执行
r“\\”
,则会打印一个
\
“\\\\\”
字符串)。小心点。是的,原始字符串文字不能以一个“\”结尾。@Blair/Thomas:谢谢-这是我遵循的一条让我一开始就感到困惑的一般规则!…现在一切都清楚了,谢谢大家。尽管遵循这条规则…当从纯文本文件读取模式时,如何将模式作为原始文字传递l string?作为一个次要的修正,
'\s'
(如
r'\s'
)也被表示为
'\\s'
,因为
'\s'
不是一个公认的转义序列。@MassoodKhaari我发誓我写这个答案时输出是正确的……修正了。8年的时间肯定证明了python行为的神奇变化是正确的。:d
print r"123\n123" 
#outputs>>>
123\n123


print "123\n123"
#outputs>>>
123
123