转义[在Python正则表达式中]

2024-04-29 20:25:00 发布

您现在位置:Python中文网/ 问答频道 /正文

此reg exp搜索将正确检查字符串是否包含以下文本:

re.search(r'\bharry\b','[harry] blah',re.IGNORECASE)

但是,我需要确保字符串包含[harry]。我试过用不同数量的反斜杠逃跑:

re.search(r'\b\[harry\]\b','[harry] blah',re.IGNORECASE)
re.search(r'\b\\[harry\\]\b','[harry] blah',re.IGNORECASE)
re.search(r'\b\\\[harry\\\]\b','[harry] blah',re.IGNORECASE)

这些解决方案都找不到匹配项。我需要做什么?

谢谢!


Tags: 字符串文本research数量解决方案regblah
3条回答

它的转义方式与大多数regex元字符的转义方式相同:前面是反斜杠。

因此,r"\[harry\]"将匹配文本字符串[harry]

问题在于模式中的\b。这是单词边界锚

\b匹配:

  • 在字符串的开头,如果它以单词字符开头
  • 在字符串的末尾,如果它以单词字符结尾
  • 在单词字符\w和非单词字符\W之间(注意大小写差异)

括号[]不是单词字符,因此如果字符串以[开头,则其左侧没有\b。任何没有\b的地方,都有\B代替(注意大小写的不同)。

参考文献

  • regular-expressions.info/Word Boundaries
  • http://docs.python.org/library/re.html

    \b : Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character. Note that \b is defined as the boundary between \w and \W, so the precise set of characters deemed to be alphanumeric depends on the values of the UNICODE and LOCALE flags. Inside a character range, \b represents the backspace character, for compatibility with Python’s string literals.

>>> re.search(r'\bharry\b','[harry] blah',re.IGNORECASE)
<_sre.SRE_Match object at 0x7f14d22df648>
>>> re.search(r'\b\[harry\]\b','[harry] blah',re.IGNORECASE)
>>> re.search(r'\[harry\]','[harry] blah',re.IGNORECASE)
<_sre.SRE_Match object at 0x7f14d22df6b0>
>>> re.search(r'\[harry\]','harry blah',re.IGNORECASE)

问题是\b,而不是括号。一个反斜杠是正确的转义。

第一个是正确的:

r'\b\[harry\]\b'

但这与[harry] blah不匹配,因为[不是单词字符,因此没有单词边界。它只在[前面有一个单词字符时匹配,就像foobar[harry] blah中一样。

相关问题 更多 >