Quantifiers: Matching Multiple 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
A period in a regular expression is not punctuation being searched for by default. It is a wildcard: it matches exactly one character of any kind. That character can be a letter, digit, space, punctuation mark, or another character. The important limit is that one period represents one character position, not an entire group of characters.
What do you think happens?
The pattern F..m: is tested against From:. What do you predict?
Reveal answer
It matches because the two periods cover the two middle characters.
The pattern has five positions: F, one character, one character, m, and :. From: has exactly those five positions. The two periods accept the characters r and o.
Fixed Structure with Flexible Middle
A useful way to read a pattern is from left to right, position by position. In F..m:, the first position must be F. The next two positions may contain any characters because they are represented by periods. The final two positions must be m and a colon. Thus, the pattern describes a five-character structure with a flexible middle.
- F matches the letter F.
- The first period matches exactly one character.
- The second period matches exactly one character.
- m matches the letter m.
- The colon matches the colon character.
Reading F..m:
Determine which of From:, Fxxm:, F12m:, and F!@m: fit the pattern F..m:.
- 1Count the pattern positions
The pattern contains five positions: F, one period position, a second period position, m, and a colon.
- 2Check the fixed positions
Each candidate begins with F and ends with m:.
- 3Check the flexible positions
The two middle positions contain r and o, x and x, 1 and 2, or ! and @. Each period accepts one of those characters.
All four strings fit F..m: because each is a five-character string that starts with F, ends with m:, and has any two characters in the middle.
Searching Lines with Python
Python's re module provides re.search() for finding a pattern within a string. When the pattern contains period wildcards, re.search() scans the string and returns a match object if the pattern is found anywhere in that string. This makes it useful for filtering text based on a known structure with variable characters.
import re
lines = ["From:", "F12m:", "Subject: Notes", "Format:"]
for line in lines:
if re.search(r"^F..m:", line):
print(line)- 1The re module supplies the search function.
- 2The pattern ^F..m: requires the match to begin at the start of the string.
- 3From:, F12m:, and Format: fit the required structure.
- 4Subject: Notes does not begin with F, so it is not printed.
- The caret anchors the pattern to the beginning of the line.
- F must appear first, followed by two characters matched by the periods.
- The text must then contain m followed by a colon.
re.search()tests each line, and matching lines are printed.
From:
F12m:
Format:From:
F12m:
Format:Wildcard or Literal Period
| Pattern form | Meaning | Accepted character |
|---|---|---|
| . | Wildcard period | Any single character |
| \. | Escaped period | The punctuation character . |
| [.] | Period inside a character class | The punctuation character . |
Use an escaped period or a period in a character class when the text must contain an actual period.
Mistakes with Period Wildcards
Common mistake Treating one period as a match for several characters.
What it looks likeUsing . when the intended pattern needs to cover two character positions.
Why it failsEach period represents exactly one character position.
Do this insteadUse multiple periods when multiple individual character positions must be matched.
Common mistake Expecting a plain period in a pattern to match only punctuation.
What it looks likeUsing . when the target text must contain the actual character .
Why it failsAn unescaped period acts as a wildcard.
Do this insteadUse \. or [.] to match a literal period.
Common mistake Forgetting that fixed characters still have to appear in the right positions.
What it looks likeTesting a string that does not start with F against F..m:.
Why it failsThe periods are flexible, but F, m, and the colon remain required by the pattern.
Do this insteadCheck both the wildcard positions and every literal character in the pattern.
Common mistake Using a pattern without the beginning anchor when the structure must start at the beginning of a line.
What it looks likeUsing F..m: instead of ^F..m: when filtering lines that must begin with F.
Why it failsThe source pattern uses ^ to anchor the match to the beginning of the line.
Do this insteadInclude ^ when the required structure must occur at the start.
Apply the Pattern
For the pattern F..m:, decide whether each string fits: F!@m:, Fm:, Fabc m:, and Fxxm:. Then explain what each period matches in every string that fits.
Hints
- Count the required positions in the pattern.
- The two periods provide exactly two character positions.
- Check that F, m, and the colon occur in their required positions.
What do you think happens?
Will re.search(r"^F..m:", "Note From:") produce a match?
Reveal answer
No.
The caret requires the pattern to begin at the start of the string. This string begins with Note rather than F.
Key Takeaways
- 1A period in a regular expression matches exactly one character of any kind.
- 2Several periods match several separate character positions; one period does not represent an entire multi-character string.
- 3Patterns such as F..m: combine fixed characters with flexible wildcard positions.
- 4Python
re.search()scans a string for a pattern and returns a match object when the pattern is found. - 5Use \. or [.] when the text must contain a literal period character.
At a glance
- The period wildcard matches one character, no more and no less.
- Multiple periods let a pattern describe multiple variable positions within a fixed structure.
re.search()can apply these patterns to strings and filter matching lines.- A plain period is a wildcard; use \. or [.] for a literal period.