Escaping Special Characters in Regular Expressions
The period (.) in a regular expression is a wildcard that matches any single character—letters, digits, spaces, punctuation, or any other character.
One Symbol, Two Meanings
A period has a special meaning in a regular expression. Written as ., it is not automatically treated as punctuation from the text being searched. Instead, it acts as a wildcard: it matches any single character. If the text must contain an actual period, the pattern must escape it as \. or place it in the character class [.].
Exactly One Character
Each period in a pattern represents exactly one character position. It can match a letter, digit, space, punctuation mark, or another character, but that one period does not represent multiple positions. To allow for multiple unknown characters, the pattern needs multiple periods.
Fixed Structure, Variable Content
Reading F..m:
Determine what the pattern F..m: allows between its fixed characters.
Read the fixed characters: The match must begin with F and end with m followed by a colon.
Read the first period: The first period accepts exactly one character in the second position.
Read the second period: The second period accepts exactly one character in the third position.
Count the positions: The pattern therefore describes a five-character string: F, one variable character, another variable character, m, and a colon.
F..m: can match From:, Fxxm:, F12m:, and F!@m:. The two middle characters may vary, but the fixed positions remain F, m, and :.
Applying re.search()
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 checking or filtering lines whose structure is known even though some characters vary.
import re lines = ["From:", "F12m:", "Subject:"] pattern = r"^F..m:" for line in lines: if re.search(pattern, line): print(line)
Escaping Literal Punctuation
Filtering Text Collections
A regular expression with period wildcards can separate text that fits a known structure from text that does not. For example, a collection of lines can be tested one line at a time with re.search(). Lines matching the pattern can be printed or retained, while lines without a match are left out of the matching result.
Mistakes with Periods
Treating an unescaped period as literal punctuation
An unescaped period is a wildcard, so it can match letters, digits, spaces, punctuation, or other single characters.
Fix:
Use \. or [.] when the period itself must occur in the text.Expecting one period to match several characters
Each period represents exactly one character position.
Fix:
Use two periods, as in F..m:, for two variable character positions.Ignoring fixed positions in a pattern
The pattern requires F first, two characters next, then m and a colon.
Fix:
Map every symbol to its position before applying the pattern.Forgetting the beginning anchor in a line-start requirement
The source pattern ^F..m: uses the caret to require the match at the beginning of the line.
Fix:
Use ^ before the pattern when the match must start at the beginning of the line.
Practice Check
For the pattern F..m:, decide whether each string fits the five-position structure: From:, F12m:, F!@m:, and Form:. Then explain what each period matched in every string that fits.
Hints
- Count the required positions: F, one character, one character, m, and :.
- The two periods may match different kinds of characters.
- The fixed characters must remain in their specified positions.
What do you think happens?
What does the pattern F..m: require from the input string?
Reveal answer
Answer: A five-character string beginning with F and ending with m:
The two periods each account for one middle character, while F, m, and the colon occupy fixed positions.
Key Takeaways
- An unescaped period is a wildcard that matches exactly one character.
- Multiple periods are needed for multiple variable character positions.
- The pattern F..m: preserves fixed positions while allowing two middle characters to vary.
- Use \. or [.] when the input must contain a literal period.
- Python re.search() can test each string or line and return a match object when the pattern is found.
Key Takeaways
- The period wildcard matches any single character at its position.
- A pattern such as F..m: describes fixed characters surrounding two variable positions.
- Use re.search() to find such patterns within strings or to filter lines.
- Escape the period as \. or use [.] when the period must be matched literally.
- Always count wildcard positions and check whether fixed characters occur in the required positions.