Search code examples
phpregexprefixsuffix

Word not starting with di and men (prefix)


I am having a problem creating a regex that suits the following criteria:

  1. prefix : Not starting with “di” and “men”
  2. Sequences of character : a-z
  3. suffix : End with: • in, at, ah, iah, • sequences of vowel (a,e,i,o,u), • then “wi”, • sequences of consonants ending with i

This is the regex that I created, but it seems doesn't work. Please help me.

$count = preg_match_all("^(^\bdi|^\bmen]\w*(in|at|ah|iah)((a|e|i|o|u)(wi)((?!(a\e\i\o\u)i$/",$value,$match);

For example, the output should capture word "lain", not "dian".


Solution

  • Try the following regex:

    ^((?!di|men)[a-z]+(in|at|ah|iah)[aeiou]*(?:wi)?[a-z]*i?)$
    

    Explanation:

    ^((?!di|men)    assert that string does not start with 'di' or 'men'
    [a-z]+          one or more sequence of characters a-z
    (in|at|ah|iah)  ends with on these patterns
    [aeiou]*        followed by optional sequence of vowels a,e,i,o,u
    wi              followed by optional 'wi'
    [a-z]*          optional sequence of characters a-z
    i?)$            optional 'i'
    

    Demo here:

    Regex 101