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.
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.
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']
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