简体   繁体   English

如何在 python 中编写正则表达式以查找所有两个辅音相邻的单词

[英]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这寻找一个

  • word break \b分词\b
  • some number of letters [az]*一些字母[az]*
  • 2 consonants [b-df-hj-np-tv-z]{2} 2 个辅音[b-df-hj-np-tv-z]{2}
  • some number of letters [az]*一些字母[az]*
  • a word break一句话中断

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.

相关问题 创建一个python正则表达式regex,它将在一个字符串中的每个单词中找到所有不会重复出现的辅音 - Create a python regular expression regex that will find all consonants in each word within a string that are not repeated one after another 查找以彼此靠近的辅音开头的单词 - Find words start with consonants that near to each other 如何在 Python 中检查两个单词是否相邻? - How to check if two words are next to each other in Python? Python 正则表达式 - 查找不同行上两个特定单词之间的所有单词 - Python Regular Expression - Find all words between two specific words on different lines 用于辅音和元音的 Python 正则表达式 - Python regular expression for consonants and vowels Python中的正则表达式如何查找成对单词 - Regular Expression in python how to find paired words Python正则表达式正则表达式,它将在单词中找到双辅音 - Python regular expression regex that will find double consonants within a word 如何在 python 中编写两个相邻的函数? - How can I write two functions next to each other in python? 如何在Python 2.7中编写正则表达式以返回字符串中的两个单词,并在它们之间使用下划线 - How to I write a regular expression in Python 2.7 to return two words in a string with an underscore between them 正则表达式:选择彼此相邻的所有两个(主题标签)单词组 - Regex: select all groups of two (hashtag) words next to each other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM