且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python正则表达式匹配整个单词

更新时间:2021-12-02 23:11:27

尝试

re.search(r'\bis\b', your_string)

来自文档:

\b 匹配空字符串,但只在单词的开头或结尾.

\b Matches the empty string, but only at the beginning or end of a word.

请注意,re 模块使用单词"的天真定义作为字母数字或下划线字符的序列",其中字母数字"取决于语言环境或 unicode 选项.

Note that the re module uses a naive definition of "word" as a "sequence of alphanumeric or underscore characters", where "alphanumeric" depends on locale or unicode options.

另请注意,如果没有原始字符串前缀,\b 被视为退格"而不是正则表达式单词边界.

Also note that without the raw string prefix, \b is seen as "backspace" instead of regex word boundary.