Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in Python to find words that follow pattern: vowel, consonant, vowel, consonant

Tags:

python

regex

Trying to learn Regex in Python to find words that have consecutive vowel-consonant or consonant-vowel combinations. How would I do this in regex? If it can't be done in Regex, is there an efficient way of doing this in Python?

like image 916
Parseltongue Avatar asked May 21 '11 06:05

Parseltongue


1 Answers

I believe you should be able to use a regular expression like this:

r"([aeiou][bcdfghjklmnpqrstvwxz])+"

for matching vowel followed by consonant and:

r"([bcdfghjklmnpqrstvwxz][aeiou])+"

for matching consonant followed by vowel. For reference, the + means it will match the largest repetition of this pattern that it can find. For example, applying the first pattern to "ababab" would return the whole string, rather than single occurences of "ab".

If you want to match one or more vowels followed by one or more consonants it might look like this:

r"([aeiou]+[bcdfghjklmnpqrstvwxz]+)+"

Hope this helps.

like image 194
Kevin Ward Avatar answered Sep 29 '22 08:09

Kevin Ward