Escapes
In regex, some characters have special meanings as we will explore across the chapters:
|
{
, }
(
, )
[
, ]
^
, $
+
, *
, ?
\
.
— Literal only within character classes.-
— Sometimes a special character within character classes.
When we wish to match these characters literally, we need to “escape” them.
This is done by prefixing the character with a \
.
Examples
Asterisk-surrounded substrings
The first and last asterisks are literal since they are escaped — \*
.
The asterisk inside the character class does not necessarily need to be escaped, but I’ve escaped it anyway for clarity.
The asterisk immediately following the character class indicates repetition of the character class, which we’ll explore in chapters that follow.