'Python regular expression for consonants and vowels
I was trying to create a regular expression which would allow any number of consonants , or any number of vowels , or a mix of consonants and vowels such that we only have any number of consonants in the beginning followed by any number of vowels ONLY, no consonant should be allowed after the vowels, it would be more clear from the example:
The following cases should pass:
TR, EE, TREE, Y, BY.
But the following should not pass the expression :
TROUBLE, OATS, TREES, IVY, TROUBLES, PRIVATE, OATEN, ORRERY.
So generally it can be visualized as : [C] [V]
C - Consonants
V - Vowels
[ ] - where the square brackets denote arbitrary presence of their contents.
And I reached upto this piece of code:
import re
def find_m(word):
if re.match("[^aeiou]*?[aeiou]*?",word):
print "PASS"
else:
print "FAIL"
find_m("tr")
find_m("ee")
find_m("tree")
find_m("y")
find_m("by")
find_m("trouble")
find_m("oats")
find_m("trees")
find_m("ivy")
find_m("aaabbbbaaa")
But it is passing for all the cases, I need a correct expressions which gives the desired results.
Solution 1:[1]
All you need to do is to add another anchor $
at the end of you regex as
if re.match("[^aeiou]*[aeiou]*$",word):
$
achors the regex at the end of the string. Allows nothing after The vowels
Note
You can drop the non greedy
?
from the regex as the non greedy does not have any effect on the specified character classThe regex would match empty strings as well.
The mandatory part in the string can be specified by replacing the
*
with+
. Say for example if the input must contain vowels, then the regex must beif re.match("[^aeiou]*[aeiou]+$",word):
Or you can check if the string is empty using a lookahead so as to ensure the string is non empty as
if re.match("^(?=.+)[^aeiou]*[aeiou]*$",word):
Test
$ cat test.py
import re
def find_m(word):
if re.match("[^aeiou]*[aeiou]*$",word):
print "PASS"
else:
print "FAIL"
find_m("tr")
find_m("ee")
find_m("tree")
find_m("y")
find_m("by")
find_m("trouble")
find_m("oats")
find_m("trees")
find_m("ivy")
find_m("aaabbbbaaa")
$ python test.py
PASS
PASS
PASS
PASS
PASS
FAIL
FAIL
FAIL
FAIL
FAIL
Solution 2:[2]
Fill in the code to check if the text passed contains the vowels a, e and i, with exactly one occurrence of any other character in between.
import re
def check_aei (text):
result = re.search(r"a.e.i", text)
return result != None
print(check_aei("academia")) # True
print(check_aei("aerial")) # False
print(check_aei("paramedic")) # True
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | georg |
Solution 2 | papi.chulo69999 |