加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式匹配仅包含特定字符的整个单词

发布时间:2020-12-14 06:06:31 所属栏目:百科 来源:网络整理
导读:我想匹配仅包含已定义字符的整个单词(或字符串). 例如,如果字母是d,o,g: dog = matchgod = matchogd = matchdogs = no match (because the string also has an "s" which is not defined)gods = no matchdoog = matchgd = match 在这句话中: dog god ogd,d
我想匹配仅包含已定义字符的整个单词(或字符串).

例如,如果字母是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

…我期望在狗,上帝和o上匹配(不是ogd,因为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 “,”
)
"""

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读