1

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.

flowdee
  • 593
  • 6
  • 11

1 Answers1

9

You can try this :

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

That is:

  • Prefix (?!
  • Postfix )
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
Raphaël Vigée
  • 2,048
  • 14
  • 27
  • Probably this is the only direct way to negate a regexp. So my vote is for you :) Negating a regexp means scanning the whole string space for strings that match the first regular expresion, and if they match, just consider they unmatched. There's no direct form to transform a regexp into its negation (or complement) One efficient algorithm is to compile the regexp to a DFA and then change all accepting states into nonaccepting states, and convert the result again back to a regexp. But normally the regexp that results from that if far much more complex than the original one. – Luis Colorado Nov 16 '15 at 12:34