0

Some characters, such as question marks and plus signs, have special meanings in regular expressions and must be preceded by a backslash if they are meant to represent the character itself. May I know which is the complete list of characters which must be preceded by a backslash ?

Is it correct to say that all non alphanumeric characters must be escaped ? And how to add a backslash to a php string , addslash() only add a slash in this few cases

  • single quote (')
  • double quote (")
  • backslash ()
  • NUL (the NUL byte)
gr68
  • 420
  • 1
  • 10
  • 25

1 Answers1

1

Actually it, depends. There are many flavors of regular expressions, most common:

  • BRE
  • ERE
  • PCRE (even it have multiple flavors through programming languages)

If you want to, you should escape meta-characters described in references above with \ , thats all. Or surround them in [], but this is kind of overkill.

Also, you can embed any UTF-8 character in PCRE (and some other flavors) via \x{FFFF} syntax, where FFFF - byte, representing codepoint

  • Talking about PHP language which is the regex flavor used ? – gr68 Jun 09 '20 at 07:49
  • 1
    @gr68 they are PCRE compatible, as i remember. Also there exists page, describing php regex meta-characters https://www.php.net/manual/en/regexp.reference.meta.php – nonForgivingJesus Jun 09 '20 at 07:51