re模块中的Python正则表达式是否支持单词边界(\b)?

时间:2021-06-13 15:43:36

While trying to learn a little more about regular expressions, a tutorial suggested that you can use the \b to match a word boundary. However, the following snippet in the Python interpreter does not work as expected:

在尝试学习更多关于正则表达式的知识时,一篇教程建议您可以使用\b来匹配单词边界。但是,Python解释器中的以下代码片段没有按照预期工作:

>>> x = 'one two three'>>> y = re.search("\btwo\b", x)

It should have been a match object if anything was matched, but it is None.

如果有匹配的对象,它应该是一个match对象,但它不是。

Is the \b expression not supported in Python or am I using it wrong?

是Python不支持\b表达式,还是我用错了?

3 个解决方案

#1


60  

Why don't you try

你为什么不试试

word = 'two're.compile(r'\b%s\b' % word, re.I)

Output:

输出:

>>> word = 'two'>>> k = re.compile(r'\b%s\b' % word, re.I)>>> x = 'one two three'>>> y = k.search( x)>>> y<_sre.SRE_Match object at 0x100418850>

Also forgot to mention, you should be using raw strings in your code

还忘了说,您应该在代码中使用原始字符串

>>> x = 'one two three'>>> y = re.search(r"\btwo\b", x)>>> y<_sre.SRE_Match object at 0x100418a58>>>> 

#2


57  

This will work: re.search(r"\btwo\b", x)

这将会工作:re.search(r"\btwo\b", x)

When you write "\b" in Python, it is a single character: "\x08". Either escape the backslash like this:

用Python编写“\b”时,只有一个字符:“\x08”。或者像这样转义反斜线:

"\\b"

or write a raw string like this:

或者像这样写一个原始字符串:

r"\b"

#3


4  

Python documentation

Python文档

https://docs.python.org/2/library/re.html#regular-expression-syntax

https://docs.python.org/2/library/re.html正则表达式语法

\b

\ 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 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, so the precise set of characters deemed to be alphanumeric depends on the values of the UNICODE and LOCALE flags. For example, r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'. Inside a character range, \b represents the backspace character, for compatibility with Python’s string literals.

匹配空字符串,但仅在单词的开头或结尾。单词被定义为字母数字或下划线字符的序列,因此单词的末尾由空格或非字母数字、非下划线字符表示。请注意,在形式上,\b被定义为\w和\w字符之间的边界(反之亦然),或者\w和字符串的开始/结束之间的边界,因此被认为是字母数字的字符的精确集合取决于UNICODE和LOCALE标志的值。例如,r'\bfoo\b'匹配'foo', 'foo。','(foo)', 'bar foo baz',但不是'foobar'或'foo3'。在字符范围内,\b表示backspace字符,以便与Python的字符串文字兼容。

#1


60  

Why don't you try

你为什么不试试

word = 'two're.compile(r'\b%s\b' % word, re.I)

Output:

输出:

>>> word = 'two'>>> k = re.compile(r'\b%s\b' % word, re.I)>>> x = 'one two three'>>> y = k.search( x)>>> y<_sre.SRE_Match object at 0x100418850>

Also forgot to mention, you should be using raw strings in your code

还忘了说,您应该在代码中使用原始字符串

>>> x = 'one two three'>>> y = re.search(r"\btwo\b", x)>>> y<_sre.SRE_Match object at 0x100418a58>>>> 

#2


57  

This will work: re.search(r"\btwo\b", x)

这将会工作:re.search(r"\btwo\b", x)

When you write "\b" in Python, it is a single character: "\x08". Either escape the backslash like this:

用Python编写“\b”时,只有一个字符:“\x08”。或者像这样转义反斜线:

"\\b"

or write a raw string like this:

或者像这样写一个原始字符串:

r"\b"

#3


4  

Python documentation

Python文档

https://docs.python.org/2/library/re.html#regular-expression-syntax

https://docs.python.org/2/library/re.html正则表达式语法

\b

\ 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 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, so the precise set of characters deemed to be alphanumeric depends on the values of the UNICODE and LOCALE flags. For example, r'\bfoo\b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'. Inside a character range, \b represents the backspace character, for compatibility with Python’s string literals.

匹配空字符串,但仅在单词的开头或结尾。单词被定义为字母数字或下划线字符的序列,因此单词的末尾由空格或非字母数字、非下划线字符表示。请注意,在形式上,\b被定义为\w和\w字符之间的边界(反之亦然),或者\w和字符串的开始/结束之间的边界,因此被认为是字母数字的字符的精确集合取决于UNICODE和LOCALE标志的值。例如,r'\bfoo\b'匹配'foo', 'foo。','(foo)', 'bar foo baz',但不是'foobar'或'foo3'。在字符范围内,\b表示backspace字符,以便与Python的字符串文字兼容。