我想匹配仅包含定义字符的整个单词(或真正的字符串)。
例如,如果字母是d
, o
, g
:
dog = match
god = match
ogd = match
dogs = no match (because the string also has an "s" which is not defined)
gods = no match
doog = match
gd = match
在这句话中:
dog god ogd, dogs o
...我希望能够匹配dog
, god
, and o
(not ogd,
因为逗号或dogs
因为s
)
这应该适合你
\b[dog]+\b(?![,])
解释
r"""
\b # Assert position at a word boundary
[dog] # Match a single character present in the list “dog”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\b # Assert position at a word boundary
(?! # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
[,] # Match the character “,”
)
"""
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)