Character Classes: Matching Specific Sets of Characters
The period (.) in a regular expression is a wildcard that matches any single character—letters, digits, spaces, punctuation, or any other character.
One Symbol, One Position
In a regular expression, a period is not automatically punctuation. When written as ., it is a wildcard that matches any single character. That character may be a letter, digit, space, punctuation mark, or another character. The important limit is quantity: one period accounts for exactly one character position.
Reading Fixed-Structure Patterns
The period wildcard is a regular expression symbol that matches any single character at its position in the pattern.
A pattern can combine fixed characters with flexible positions. In F..m:, the first character must be F. Each period then accepts one character. The final two characters must be m:. Because there are two periods, the pattern allows exactly two characters between F and m:. The middle characters may be letters, digits, punctuation, spaces, or other characters.
Testing F..m:
Determine which source strings fit the pattern F..m:.
From:: It begins with F, contains two middle characters, and ends with m:. It fits the pattern.
Fxxm:: The two x characters occupy the two period positions. It fits the pattern.
F12m:: The digits 1 and 2 occupy the two period positions. It fits the pattern.
F!@m:: The punctuation characters occupy the two period positions. It fits the pattern.
F..m: matches any five-character string that starts with F, ends with m:, and has exactly two characters in the middle.
Applying Patterns with re.search()
Python's re module provides re.search() for finding a pattern within a string. When the pattern is found, re.search() returns a match object. If the pattern is not found, it produces no match. This makes it useful for checking lines and filtering text based on a known structure with variable characters.
From:
F12m:
Fxxm:The caret at the beginning of ^F..m: anchors the pattern to the beginning of the line. The expression therefore checks for F, two single-character positions, and m: at the start of each line. The line Subject: update does not fit that structure, so it is not printed.
Wildcard or Literal Period
A period has two different roles in a regular expression. An unescaped period, written as ., is a wildcard and can match any one character. To require an actual period character in the text, escape it as \. or place it inside a character class as [.].
| Pattern | Meaning | Text that can match |
|---|---|---|
| . | Any single character | A, 7, !, or a space |
| \. | A literal period character | . |
| [.] | A literal period character inside a character class | . |
Mistakes with Character Positions
Treating one period as a match for several characters.
Each period represents exactly one character position.
Fix:
Use F..m: when two variable character positions are required.Expecting F..m: to match a string with no middle characters.
The pattern contains two periods, so it requires two character positions between F and m:.
Fix:
Check that the target has exactly the positions represented by the pattern.Using an unescaped period when the text must contain an actual period.
An unescaped period is a wildcard and can match any single character.
Fix:
Use \. or [.].Assuming re.search() checks only the beginning of a line without an anchor.
The caret is the part of the source pattern that anchors the match to the beginning of the line.
Fix:
Use ^F..m: when the required structure must start at the beginning.
Pattern-Building Practice
What do you think happens?
Which of these strings fit the pattern F..m:?
Reveal answer
Answer: From:, F12m:, and Fxxm: fit the pattern. Fm: does not, because the pattern requires two characters in the two period positions.
The fixed parts are F at the beginning and m: at the end. Each of the two periods must match one character.
Write a regular expression pattern that matches an actual period character rather than any character. Then explain why an unescaped period would be too broad.
Hints
- The wildcard form is a single unescaped period.
- The literal form uses an escaped period or a period inside a character class.
For the pattern ^F..m:, identify the fixed characters, the wildcard positions, and the role of re.search() when it is applied to a line.
Hints
- The caret anchors the pattern to the beginning.
- The two periods represent two single-character positions.
- re.search() returns a match object when the pattern is found.
Key Takeaways
- An unescaped period is a wildcard that matches exactly one character.
- Multiple periods are required for multiple variable character positions.
- Fixed characters and period wildcards can describe strings with stable structure and changing content.
- re.search() applies a pattern to text and returns a match object when the pattern is found.
- Use \. or [.] when the pattern must match an actual period character.
Key Takeaways
- The period wildcard matches any single character, including letters, digits, spaces, and punctuation.
- Each period represents exactly one character position.
- Patterns such as F..m: combine fixed characters with two variable positions.
- Python re.search() can find these patterns in text and support filtering.
- Escape the period with \. or use [.] when matching a literal period.