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
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:.
Count the pattern positions: The pattern contains five positions: F, one period position, a second period position, m, and a colon.
Check the fixed positions: Each candidate begins with F and ends with m:.
Check 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)
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 . |
Mistakes with Period Wildcards
Treating one period as a match for several characters.
Each period represents exactly one character position.
Fix:
Use multiple periods when multiple individual character positions must be matched.Expecting a plain period in a pattern to match only punctuation.
An unescaped period acts as a wildcard.
Fix:
Use \. or [.] to match a literal period.Forgetting that fixed characters still have to appear in the right positions.
The periods are flexible, but F, m, and the colon remain required by the pattern.
Fix:
Check both the wildcard positions and every literal character in the pattern.Using a pattern without the beginning anchor when the structure must start at the beginning of a line.
The source pattern uses ^ to anchor the match to the beginning of the line.
Fix:
Include ^ 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
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
- A period in a regular expression matches exactly one character of any kind.
- Several periods match several separate character positions; one period does not represent an entire multi-character string.
- Patterns such as F..m: combine fixed characters with flexible wildcard positions.
- Python re.search() scans a string for a pattern and returns a match object when the pattern is found.
- Use \. or [.] when the text must contain a literal period character.
Key Takeaways
- 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.