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:.
- 1From
It begins with F, contains two middle characters, and ends with m:. It fits the pattern.
- 2Fxxm
The two x characters occupy the two period positions. It fits the pattern.
- 3F12m
The digits 1 and 2 occupy the two period positions. It fits the pattern.
- 4F!@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 | . |
The pattern must express whether the period is flexible or literal.
Mistakes with Character Positions
Common mistake Treating one period as a match for several characters.
What it looks likeUsing F.m: when the intended structure has two variable characters between F and m:.
Why it failsEach period represents exactly one character position.
Do this insteadUse F..m: when two variable character positions are required.
Common mistake Expecting F..m: to match a string with no middle characters.
What it looks likeFm:Why it failsThe pattern contains two periods, so it requires two character positions between F and m:.
Do this insteadCheck that the target has exactly the positions represented by the pattern.
Common mistake Using an unescaped period when the text must contain an actual period.
What it looks likeUsing . when searching specifically for the punctuation character .
Why it failsAn unescaped period is a wildcard and can match any single character.
Do this insteadUse \. or [.].Common mistake Assuming re.search() checks only the beginning of a line without an anchor.
What it looks likeSearching with F..m: instead of ^F..m: when the structure must begin the line.
Why it failsThe caret is the part of the source pattern that anchors the match to the beginning of the line.
Do this insteadUse ^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
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
- 1An unescaped period is a wildcard that matches exactly one character.
- 2Multiple periods are required for multiple variable character positions.
- 3Fixed characters and period wildcards can describe strings with stable structure and changing content.
- 4
re.search()applies a pattern to text and returns a match object when the pattern is found. - 5Use \. or [.] when the pattern must match an actual period character.
At a glance
- 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.