Search code examples
phpregexregex-negationregex-lookaroundsnegation

Negate complete regex


Forcing a certain url structure "type/name" and my working regex is:

([a-z]+\/?[a-z]+\/?)+

Now I want to remove all other characters from the string via preg_replace and therefore negate the pattern above. Simple cases like [^a-z] work fine but for my pattern I don't get it working by negating the whole pattern.


Solution

  • You can try this :

    (?!([a-z]+\/?[a-z]+\/?)+)
    

    That is:

    • Prefix (?!
    • Postfix )