擅长:python、mysql、java
<p>在使用反斜杠转义符的情况下,这种区别将变得明显:</p>
<pre><code>>>> s="foobar"
>>> import re
>>> re.sub('(o)\1', '', s) # Using the backreference has no effect here as it's interpreted as a literal escaped 1
'foobar'
>>> re.sub(r'(o)\1', '', s) # Using the backreference works!
'fbar'
>>> re.sub('(o)\\1', '', s) # You need to escape the backslash here
'fbar'
</code></pre>
<p>引自<a href="http://en.wikipedia.org/wiki/String_literal#Raw_strings" rel="nofollow">String literal</a>:</p>
<blockquote>
<p>A few languages provide a method of specifying that a literal is to be
processed without any language-specific interpretation. This avoids
the need for escaping, and yields more legible strings.</p>
</blockquote>
<p>您可能还想引用<a href="http://docs.python.org/2/reference/lexical_analysis.html#string-literals" rel="nofollow">Lexical Analysis</a>。在</p>