find words that matches the 3 consecutive vowels Regex
Question:
text = "Life is beautiful"
pattern = r"[aeiou]{3,}"
result = re.findall(pattern, text)
print(result)
desired result:
['beautiful']
the output I get:
['eau']
I have tried googling and etc….I found multiple answers but none of them worked!!
I am new to regex so maybe I am having issues but I am not sure how to get this to out
I have tried using r"b[abcde]{3,}b"
still nothing SO please help!!
Answers:
Your regex only captures the 3 consecutive vowels, so you need to expand it to capture the rest of the word. This can be done by looking for a sequence of letters between two word breaks and using a positive lookahead for 3 consecutive vowels within the sequence. For example:
import re
text = "Life is beautiful"
pattern = r"b(?=[a-z]*[aeiou]{3})[a-z]+b"
result = re.findall(pattern, text, re.I)
print(result)
Output:
['beautiful']
A little improvement on the former solution would be using w instead a-z as the character classes (This will match lower and uppercase letters)
b[w]+[aeiou]{3,}[w]+b
Cheers!
pattern=r"bw*[aeiou]{3,}w*b"
w* For any alpha Alphanumerics that "could" exist before and after the vowels
I know it’s a late reply but just wanted to share this for whoever searches this up!
Answer:
pattern = r"b[a-zA-Z][aeiou]{3,}[a-z]b" OR experimenting with [w]* instead of [a-zA-Z]
change the first match from [a-z] to [a-zA-Z]
text = "Life is beautiful"
pattern = r"[aeiou]{3,}"
result = re.findall(pattern, text)
print(result)
desired result:
['beautiful']
the output I get:
['eau']
I have tried googling and etc….I found multiple answers but none of them worked!!
I am new to regex so maybe I am having issues but I am not sure how to get this to out
I have tried using r"b[abcde]{3,}b"
still nothing SO please help!!
Your regex only captures the 3 consecutive vowels, so you need to expand it to capture the rest of the word. This can be done by looking for a sequence of letters between two word breaks and using a positive lookahead for 3 consecutive vowels within the sequence. For example:
import re
text = "Life is beautiful"
pattern = r"b(?=[a-z]*[aeiou]{3})[a-z]+b"
result = re.findall(pattern, text, re.I)
print(result)
Output:
['beautiful']
A little improvement on the former solution would be using w instead a-z as the character classes (This will match lower and uppercase letters)
b[w]+[aeiou]{3,}[w]+b
Cheers!
pattern=r"bw*[aeiou]{3,}w*b"
w* For any alpha Alphanumerics that "could" exist before and after the vowels
I know it’s a late reply but just wanted to share this for whoever searches this up!
Answer:
pattern = r"b[a-zA-Z][aeiou]{3,}[a-z]b" OR experimenting with [w]* instead of [a-zA-Z]
change the first match from [a-z] to [a-zA-Z]