繁体   English   中英

查找仅包含特定字符的字符串中的单词

[英]Find words in strings which contain specific characters only

我正在尝试编写正则表达式以查找仅包含特定字符的单词。

对于前:

text= "I want to mes a message saying mess"

我希望正则表达式查找仅包含字符“ m”,“ e”,“ s”(即“ mes”和“ mess”)的单词。

我不希望正则表达式查找消息,因为它包含“ m”,“ e”,“ s”之外的其他字符。

reg= r"(?:[mes"]){1,}是要尝试的...。

还可以请我帮我写一个正则表达式,它包含以我开头的单词,但不包含像menat这样的单词

text=" Regex should find mess mean and all words starting with me except men and meal"

输出应仅为: mess mean me

谢谢...

我认为是\\b[mes]+\\b但我认为还有更多方法可以做到这一点

这是我的方式,没有正则表达式

text = "I want to mes a message saying mess".split()
rtext = [t for t in text if t.find("me") == 0] # only find word begin with `me`
xtext = [t for t in text if "me" in t] #May be too broad
print(rtext)

还可以请我帮我写一个正则表达式,它包含以我开头的单词,但不包含像menat这样的单词

是。 尝试以下方法:

(?!(\bmeal\b)|(\bmen\b))\bme\w+

看到这个链接的解释和演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM