Chapter 2: Meta Characters
Overview of meta characters
Meta characters are special symbols in a regular expression that stand for types of characters or control how matching works. You need to know them to write effective patterns, and this chapter introduces the main ones.
Character classes and sets
Example 1: Character class
A character class matches any single character within the brackets.
[abc]
This matches ‘a’, ‘b’, or ‘c’.
Example 2: Character range
A character range specifies a range of characters to match.
[a-z]
This matches any lowercase letter from ‘a’ to ‘z’.
Example 3: Negated character class
A negated character class matches any character not in the brackets.
[^abc]
This matches any character except ‘a’, ‘b’, or ‘c’.
Anchors and boundaries
Example 1: Start of string anchor
The caret (^) matches the start of a string.
^start
This matches ‘start’ only if it is at the beginning of a line.
Example 2: End of string anchor
The dollar sign ($) matches the end of a string.
end$
This matches ‘end’ only if it is at the end of a line.
Example 3: Word boundary
The word boundary (\b) matches the position between a word and a non-word character.
\bword\b
This matches ‘word’ as a whole word, not as part of another word like ‘wording’.
Quantifiers
Example 1: Zero or more
The asterisk (*) matches zero or more occurrences of the preceding element.
a*
This matches ‘’, ‘a’, ‘aa’, ‘aaa’, and so on.
Example 2: One or more
The plus sign (+) matches one or more occurrences of the preceding element.
a+
This matches ‘a’, ‘aa’, ‘aaa’, and so on, but not an empty string.
Example 3: Zero or one
The question mark (?) matches zero or one occurrence of the preceding element.
a?
This matches ‘’ or ‘a’.
Example 4: Specific number of occurrences
Braces ({n,m}) match at least n and at most m occurrences of the preceding element.
a{2,3}
This matches ‘aa’ or ‘aaa’.
Grouping and capturing
Example 1: Grouping
Parentheses (()) group elements together.
(abc)
This matches ‘abc’.
Example 2: Capturing groups
Parentheses also capture the matched text for later use.
(\d{3})-(\d{2})-(\d{4})
This matches a Social Security number format and captures each group of digits.
Escape sequences
Example 1: Escaping special characters
The backslash (\) escapes a special character so that it matches literally.
\.
This matches a literal dot.
Example 2: Common escape sequences
\d: Matches any digit, equivalent to[0-9].\D: Matches any non-digit.\w: Matches any word character (alphanumeric + underscore), equivalent to[a-zA-Z0-9_].\W: Matches any non-word character.\s: Matches any whitespace character (space, tab, newline).\S: Matches any non-whitespace character.
Lookahead and lookbehind
Example 1: Positive lookahead
The positive lookahead ((?=...)) checks that the characters that follow match the given pattern, without consuming them.
a(?=b)
This matches ‘a’ only if it is followed by ‘b’.
Example 2: Negative lookahead
The negative lookahead ((?!...)) checks that the characters that follow do not match the given pattern.
a(?!b)
This matches ‘a’ only if it is not followed by ‘b’.
Example 3: Positive lookbehind
The positive lookbehind ((?<=...)) checks that the characters before the current position match the given pattern, without consuming them.
(?<=b)a
This matches ‘a’ only if it is preceded by ‘b’.
Example 4: Negative lookbehind
The negative lookbehind ((?<!...)) checks that the characters before the current position do not match the given pattern.
(?<!b)a
This matches ‘a’ only if it is not preceded by ‘b’.