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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With