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)
CodePudding user response:
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
- some number of letters
[a-z]*
- 2 consonants
[b-df-hj-np-tv-z]{2}
- some number of letters
[a-z]*
- a word break
I would look for two consonants specifically to avoid having to worry about matching (for example) fuel.
In 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.
Result (for your data):
['black', 'stop', 'double']
CodePudding user response:
\b\w*(?:(?![aeiou0-9])\w){2}\w*\b
You can try this.See demo.
https://regex101.com/r/36VzAk/1
CodePudding user response:
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']
CodePudding user response:
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*
will look for any word character, zero or more times[^aeiou\d\s]{2,}
will look for at least two consecutive consonants (any non-digit, non-whitespace, non-vowels characters)\w*
will again look for any word character, zero or more times
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