Dot (.) and backslash (\)

Some characters have one meaning in regular expressions and completely different meanings in other contexts. For example, in regular expressions, the dot (.) is a special character used to match any one character. In written language, the period (.) is used to indicate the end of a sentence. In mathematics, the decimal point (.) is used to separate the whole part of a number from the fractional part.

Regular expressions first evaluates a special character in the context of regular expressions: if it sees a dot, then it knows to match any one character.

For example, the regular expression 1. matches:

  • 11
  • 1A

The regular expression 1.1 matches

  • 111
  • 1A1

If you were to provide an IP address as a regular expression, you would get unpredictable results. For example, the regular expression 0.0.0.0 matches:

  • 0102030
  • 0a0b0c0

In order to tell regular expressions to see the dot in its original context as a separator for the different parts of the IP address and not as a special character used to match any other character, you need to provide a signal to that effect. The backslash (\) is that signal. When regular expressions sees a backslash, it knows that it should interpret the next character literally. A regular expression to match the IP address 0.0.0.0 would be:

0\.0\.0\.0

Use the backslash to escape any special character and interpret it literally; for example:

  • \\ (escapes the backslash)
  • \[ (escapes the bracket)
  • \{ (escapes the curly brace)
  • \. (escapes the dot)
Was this article helpful?
How can we improve it?