Skip to content
Advertisement

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.

Advertisement

Answer

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 class

  • The 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 be

      if 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
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement