notHalfBad
notHalfBad

Reputation: 213

Regex: How to match words without consecutive vowels?

I'm really new to regex and I've been able to find regex which can match this quite easily, but I am unsure how to only match words without it.

I have a .txt file with words like

sheep
fleece
eggs
meat
potato

I want to make a regular expression that matches words in which vowels are not repeated consecutively, so it would return eggs meat potato.

I'm not very experienced with regex and I've been unable to find anything about how to do this online, so it'd be awesome if someone with more experience could help me out. Thanks!

I'm using python and have been testing my regex with https://regex101.com.

Thanks!

EDIT: provided incorrect examples of results for the regular expression. Fixed.

Upvotes: 4

Views: 2581

Answers (2)

John1024
John1024

Reputation: 113884

Note that, since the desired output includes meat but not fleece, desired words are allowed to have repeated vowels, just not the same vowel repeated.

To select lines with no repeated vowel:

>>> [w for w in open('file.txt') if not re.search(r'([aeiou])\1', w)]
['eggs\n', 'meat\n', 'potato\n']

The regex [aeiou] matches any vowel (you can include y if you like). The regex ([aeiou])\1 matches any vowel followed by the same vowel. Thus, not re.search(r'([aeiou])\1', w) is true only for strings w that contain no repeated vowels.

Addendum

If we wanted to exclude meat because it has two vowels in a row, even though they are not the same vowel, then:

>>> [w for w in open('file.txt') if not re.search(r'[aeiou]{2}', w)]
['eggs\n', 'potato\n']

Upvotes: 9

mouse_s
mouse_s

Reputation: 58

@John1024 's answer should work I also would try

"\w*(a{2,}|e{2,}|i{2,}|o{2,}|u{2,})\w*"ig

Upvotes: 0

Related Questions