<p>Python将\uxxx视为字符串文本中的unicode字符转义(例如,u“\u2014”被解释为unicode字符u+2014)。但我刚刚发现(Python2.7)标准regex模块没有将\uxxx视为unicode字符。示例:</p>
<pre><code>codepoint = 2014 # Say I got this dynamically from somewhere
test = u"This string ends with \u2014"
pattern = r"\u%s$" % codepoint
assert(pattern[-5:] == "2014$") # Ends with an escape sequence for U+2014
assert(re.search(pattern, test) != None) # Failure -- No match (bad)
assert(re.search(pattern, "u2014")!= None) # Success -- This matches (bad)
</code></pre>
<p>显然,如果您能够将正则表达式模式指定为字符串文本,那么您可以产生与正则表达式引擎本身可理解的\uxxx转义符相同的效果:</p>
^{pr2}$
<p>但是如果你需要动态地构建你的模式呢?在</p>