[英]how to write regular expression in python to find all Words with two consonants next to each other
I tried this code but does not work我试过这段代码但不起作用
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)
I would use the following regex:我会使用以下正则表达式:
\b[a-z]*[b-df-hj-np-tv-z]{2}[a-z]*\b
This looks for a这寻找一个
\b
分词\b
[az]*
一些字母[az]*
[b-df-hj-np-tv-z]{2}
2 个辅音[b-df-hj-np-tv-z]{2}
[az]*
一些字母[az]*
I would look for two consonants specifically to avoid having to worry about matching (for example) fuel.
我会专门寻找两个辅音,以避免担心匹配(例如) fuel.
In python:在 python 中:
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)
Use the re.I
flag to avoid having to specify both upper and lower case letters in the character classes.使用re.I
标志可以避免在字符类中同时指定大写和小写字母。
Result (for your data):结果(针对您的数据):
['black', 'stop', 'double']
\b\w*(?:(?![aeiou0-9])\w){2}\w*\b
You can try this.See demo.你可以试试这个。看演示。
https://regex101.com/r/36VzAk/1 https://regex101.com/r/36VzAk/1
This should work:这应该工作:
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)
prints:印刷:
['black', 'stop', 'double']
Assuming you want to retrieve the whole words having two consonants next to each other, and ignoring digits, you could use this regex (\w*[^aeiou\d\s]{2,}\w*)
.假设您想要检索两个辅音相邻的整个单词,并忽略数字,您可以使用此正则表达式(\w*[^aeiou\d\s]{2,}\w*)
。
\w*
will look for any word character, zero or more times \w*
将查找任何单词字符,零次或多次[^aeiou\d\s]{2,}
will look for at least two consecutive consonants (any non-digit, non-whitespace, non-vowels characters) [^aeiou\d\s]{2,}
将查找至少两个连续的辅音(任何非数字、非空白、非元音字符)\w*
will again look for any word character, zero or more times \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)
Outputs产出
black
stop
double
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.