python regex查找“cat”而不是“catfish”或“caterpillar”,

2024-05-21 11:56:57 发布

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

我不太习惯regex,我很难创建一个在字符串中任何地方都可以找到“cat”后跟(或不后跟)任何标点符号,但不能找到“caterpillar”、“catfish”等


Tags: 字符串caterpillar地方regexcat习惯标点符号catfish
1条回答
网友
1楼 · 发布于 2024-05-21 11:56:57

在Python正则表达式中,\b是一个单词边界,因此您可以搜索cat\b(尽管这也会选择像bobcattomcat这样的内容,因此如果不需要,您可能需要使用\bcat\b)。你知道吗

Python 3.4 docs(尽管2.7是very similar):

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

相关问题 更多 >