The Matching Characters
Explore how to match characters using Python's regular expressions, focusing on metacharacters like character classes, repetition symbols, and anchors. Understand how to use square brackets for character sets, the star and plus for repetitions, optional matching with question marks, and range quantifiers to precisely control pattern matching.
Overview
When we want to match a character in a string, in most cases we can just use that character or that substring.
So if we wanted to match
“dog”, then we would use the letters dog. Of course, there are some
characters that are reserved for regular expressions. These are known as
metacharacters. The following is a complete list of the metacharacters
that Python’s regular expression implementation supports:
. ^ $ * + ? { } [ ] | ( )
Let’s spend a few moments looking at how some of these work.
[ and ]
One of the most common pairs of metacharacters we will encounter are the square braces: [ and ]. They are used for creating a “character class”, which is a set of characters that we would like to match.
We may list the characters individually like this: [xyz]. This will ...