http://php.net/manual/en/function.preg-quote.php:
The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) {
} = ! < > | : –
However this page says that special characters are [ \ ^ $ . | ? * + ( )
Ok I know that the first page is specifically on php regular expressions. However why do we need to escape the !, <, >, :, =, - ?
I tried to do a preg_match without escaping <, >, - and ! and everything is working perfectly.
Those characters are metacharacters, but they need no escaping. What they do have in common is that they occur in special grouping constructs:
But they only take on a special meaning in this context. So if you take any string and escape all these characters:
[\^$.|?*+(){, then you get a regex that will exactly match the string character by character because those other metacharacters can never be in a meta-context.For example, the
]is only a metacharacter if there was a previous unescaped[that opened a character class.Similarly, the
-is only a metacharacter in a character class, meaning “range” as in[a-z](or a literal-as in[abc-].So to escape the string
[tag-soup]you just need to escape the[. Outside of a character class,]and-are simply treated as literals.In summary, if you take a string and escape all the “unconditional” metacharacters (
[\^$.|?*+(){) then you get a regex that will exactly match the string character by character.