应该使用php正则表达式中的反斜杠转义哪些符号?

preg_match('@\.php@',$url,$match)

Common:-

  • slash
  • quotation
  • double quotation

any others?

Should @, ?, = be escaped?

There is a list of special Regex characters in the PHP documentation here: http://php.net/manual/en/function.preg-quote.php

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

Perhaps you will find the preg_quote function useful:

preg_quote() takes str and puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

@ and ? should be backslashified, = should not.

The characters you need to backslash escape in a regex include:

  • Square brackets []
  • Parentheses ()
  • Curly braces {}
  • Caret ^
  • Dollar sign $
  • Period .
  • Pipe |
  • Asterisk *
  • Plus +
  • Question mark ?
  • Backslashes \

Additionally, your regex delimiter, in this case @, needs to be backslashified.

It is important to note that certain characters may be metacharacters in a certain context. For example, the hyphen is not a metacharacter in the regex denoted by this string:

"/foo-/"

But is a metacharacter in the following string:

"/foo[a-z]/"