0

I am trying to formulate a regex expression (running in Python) that gets passed a word and needs to only find words that do not contain 2 adjacent vowels. For Example:

me - would match
mee - would not match
meat - would not match
base - would match
basketball - would match

I am lost here as I do not know how to check for something that does not exist?

Thanks for the help

jfs
  • 399,953
  • 195
  • 994
  • 1,670
CoreCode
  • 2,227
  • 4
  • 22
  • 24
  • does it have to be regex? strings can act like lists, and so can be accessed as lists, you could feed a function a word and then parse it as a list, if two consecutive items are vowels, then it would fail, should i write this code for you? – Inbar Rose Aug 13 '12 at 13:06
  • related: [Regex: Matching by exclusion, without look-ahead - is it possible?](http://stackoverflow.com/q/466053/4279) – jfs Aug 13 '12 at 13:19
  • @InbarRose yeah I would rather use regex for this. This is more of a learning thing than a real development project – CoreCode Aug 13 '12 at 13:26

2 Answers2

4
import re

r = re.compile("[aeiou][aeiou]")
m = r.search("me")   # => None
m = r.search("mee")  # => Matcher
m = r.search("meat") # => Matcher
m = r.search("base") # => None

So if not m is True for all cases that don't match.

  • if m is not True probably looks nicer, and you might consider using that instead. – hifkanotiks Aug 13 '12 at 13:16
  • you should mention that it does the opposite i.e., it matches words that *do* contain adjacent vowels. It is a valid solution because It is simple to negate the match in the code later. – jfs Aug 13 '12 at 13:22
  • But Remember I am looking for the exact opposite of what you have done there. I am looking for the ones with adjacent vowels to not match – CoreCode Aug 13 '12 at 13:23
3
m = re.match(r"(?:[^euioa]|[euioa](?![euioa]))*$", word)

@Tichodroma's answer is simpler and therefore should be preferable if you can negate the match in the code later i.e., just write if not m where you would write if m with this solution.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670