Regexp - Escape character (Backslash)

> Procedural Languages > Multilingual Regular Expression Syntax (Pattern)

1 - About

Backslash is the general escape meta-character with several uses. It can:

  • Stand for itself,
  • Quote the next character,
  • Introduce an operator,
  • Do nothing
Advertising

3 - Use Case

3.1 - Escape Meta_character

It

  • takes away any special meaning that character may have.
  • applies both inside and outside character classes.

For example, if you want to match:

  • a backslash, you write “\\”.
  • a “*” character, you write “\*”

otherwise, they would be have be interpreted as meta-character

Php: Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \\, then “\\\\” or '\\\\' must be used in PHP code.

3.2 - Non-printing characters

encoding non-printing characters

You can also use the binary character (in Unicode) but the below one are easier. (binary zero terminates a pattern):

Encoding Description
\a alarm, that is, the BEL character (hex 07)
\cx “control-x”, where x is any character
\e escape (hex 1B)
\f formfeed (hex 0C)
\n newline (hex 0A)
\p{xx} a character with the xx property, see unicode properties for more info
\P{xx} a character without the xx property, see unicode properties for more info
\r carriage return (hex 0D)
\t tab (hex 09)
\xhh character with hex code hh
\ddd character with octal code ddd, or backreference

See the documentation of the regexp function.

Advertising

3.3 - Generic character types

The third use of backslash is for specifying generic character types.

3.4 - Simple Assertions

An assertion specifies a condition that has to be met at a particular point in a match, without consuming any characters from the subject string.

assertions Description
\b word boundary
\B not a word boundary
\A start of subject (independent of multiline mode)
\Z end of subject or newline at end (independent of multiline mode)
\z end of subject (independent of multiline mode)
\G first matching position in subject

These assertions may not appear in character classes (but note that “\b” has a different meaning, namely the backspace character, inside a character class).

4 - Documentation / Reference

Advertising