如何在python中编写正则表达式来查找所有两个辅音相邻的单词
python 241
原文标题 :how to write regular expression in python to find all Words with two consonants next to each other
我尝试了这段代码但不起作用
string2 = "eat black eat eaten name 1234 stop double " //sample of string
result62 = re.findall(r'\b\w+[^ 0-9][^ aeiouAEIOU]{2}\b', string2)
print(result62)
回复
我来回复-
Nick 评论
我会使用以下正则表达式:
\b[a-z]*[b-df-hj-np-tv-z]{2}[a-z]*\b
这寻找一个
- 断字\b
- 一些字母 [a-z]*
- 2 个辅音 [b-df-hj-np-tv-z]{2}
- 一些字母 [a-z]*
- 一个单词中断
我会专门寻找两个辅音以避免担心匹配(例如)
fuel.
在蟒蛇中:
string2 = "eat black eat eaten name 1234 stop double fuel." result62 = re.findall(r'\b[a-z]*[b-df-hj-np-tv-z]{2}[a-z]*\b', string2, re.I)
使用
re.I
标志以避免在字符类中同时指定大写和小写字母。结果(对于您的数据):
['black', 'stop', 'double']
2年前 -
vks 评论2年前
-
Sadra Naddaf 评论
这应该有效:
string2 = "eat black eat eaten name 1234 stop double " result62 = re.findall(r'\b(?=[a-z]*(?:(?![aeiou])[a-z]){2})[a-z]*\b', string2) print(result62)
印刷:
['black', 'stop', 'double']
2年前 -
Antoine Delia 评论
假设您要检索具有两个相邻辅音并忽略数字的整个单词,您可以使用此正则表达式
(\w*[^aeiou\d\s]{2,}\w*)
。- \w* 将查找任何单词字符,零次或多次
- [^aeiou\d\s]{2,} 将查找至少两个连续的辅音(任何非数字、非空白、非元音字符)
- \w* 将再次查找任何单词字符,零次或多次
import re my_string = "eat black eat eaten name 1234 stop double" my_re = re.compile(r"(\w*[^aeiou\d\s]{2,}\w*)", re.I) matches = my_re.findall(my_string) for match in matches: print(match)
输出
black stop double
2年前