本文介绍了正则表达式匹配仅包含某些字符的整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想匹配仅包含已定义字符的整个单词(或真正的字符串).
I want to match entire words (or strings really) that containing only defined characters.
例如如果字母是 d
, o
, g
:
For example if the letters are 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
和 o
(不是 ogd、
由于逗号或 dogs
由于 s
)
...I would expect to match on dog
, god
, and o
(not ogd,
because of the comma or dogs
due to the 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 ","
)
"""
这篇关于正则表达式匹配仅包含某些字符的整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!