Search code examples
regexpcre

How to match all alphabet except few?


I want to match [a-z] only except the letters a,e,i,o,u

Using negated set [^aeiou]* I could match everything except a,e,i,o,u, but how to restrict my everything to [a-z]?

This can be easily done using character class subtraction ([a-z-[aeiou]]) in XML Schema, XPath, .NET (2.0+), and JGsoft regex flavors, but how can I do it in PCRE?


Solution

  • You could use negative lookahead assertion. It's like a kind of subtraction.

    (?![aeiou])[a-z]
         ^        ^
         |        |
    subtract    from
    

    DEMO