\b - Matches the empty string, but only at the beginning or end of a word.
A word is defined as a sequence of Unicode alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore Unicode character.
Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning/end of the string. This means that r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.
By default Unicode alphanumerics are the ones used, but this can be changed by using the ASCII flag. Inside a character range, \b represents the backspace character, for compatibility with Python’s string literals.
在Python正则表达式中,
\b
是一个单词边界,因此您可以搜索cat\b
(尽管这也会选择像bobcat
或tomcat
这样的内容,因此如果不需要,您可能需要使用\bcat\b
)。你知道吗从Python 3.4 docs(尽管2.7是very similar):
相关问题 更多 >
编程相关推荐