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

编程入门 行业动态 更新时间:2024-10-11 13:19:39
本文介绍了正则表达式匹配仅包含某些字符的整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想匹配仅包含已定义字符的整个单词(或真正的字符串).

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 "," ) """

更多推荐

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

本文发布于:2023-11-02 12:09:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   字符   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!