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

在尝试了解有关正则表达式的更多信息时,一个教程建议您可以使用\b来匹配单词边界。但是,Python解释器中的以下代码片段无法按预期工作:

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

如果有任何匹配项,它应该是一个匹配对象,但它是None

\bPython不支持该表达式吗?或者我使用的是错误的?

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:

>>> 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.

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


回答 0

你为什么不尝试

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

输出:

>>> 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>

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

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

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>
>>> 

回答 1

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

"\b"用Python 编写时,它是一个字符:"\x08"。可以这样逃避反斜杠:

"\\b"

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

r"\b"

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

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

"\\b"

or write a raw string like this:

r"\b"

回答 2

只是为了明确解释为什么 re.search("\btwo\b", x)不起作用,这是因为\b在Python字符串中,Backspace字符是简写形式。

print("foo\bbar")
fobar

因此,模式"\btwo\b"正在寻找一个空格,其次是two,之后是另一个空格,您在(x = 'one two three')中搜索的字符串没有空格。

要允许re.search(或compile)将序列解释\b为单词边界,请转义反斜杠("\\btwo\\b")或使用原始字符串创建模式(r"\btwo\b")。

Just to explicitly explain why re.search("\btwo\b", x) doesn’t work, it’s because \b in a Python string is shorthand for a backspace character.

print("foo\bbar")
fobar

So the pattern "\btwo\b" is looking for a backspace, followed by two, followed by another backspace, which the string you’re searching in (x = 'one two three') doesn’t have.

To allow re.search (or compile) to interpret the sequence \b as a word boundary, either escape the backslashes ("\\btwo\\b") or use a raw string to create your pattern (r"\btwo\b").


回答 3

Python文档

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

\ b

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

Python documentation

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

\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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。