Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: How to match words without consecutive vowels?

Tags:

python

regex

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.

like image 389
notHalfBad Avatar asked Dec 25 '22 02:12

notHalfBad


1 Answers

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']
like image 131
John1024 Avatar answered Dec 26 '22 16:12

John1024