Do regular expressions from the re module support word boundaries (\b)?

前端 未结 4 609
闹比i
闹比i 2020-11-22 03:38

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 sn

4条回答
  •  难免孤独
    2020-11-22 03:53

    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.

提交回复
热议问题