Anchors: Matching at the Start or End of a Line
Without that beginning anchor, re.search() can find the matching structure later within a longer string.
A Fixed Shape with Flexible Positions
Regular expressions are useful when part of a string has a known structure and another part can vary. The period character, written as ., supplies that flexibility: in a regular expression, each period matches exactly one character. A pattern can therefore require particular characters while allowing other positions to contain letters, digits, spaces, punctuation, or any other character.
What do you think happens?
What does the pattern F..m: require?
Reveal answer
A five-character string beginning with F and ending with m:
The first character must be F, each of the two periods consumes one character, and the final two characters must be m and a colon.
Counting Character Positions
Treat every period as one position in the target text. In F..m:, the pattern has five positions: F, one wildcard position, a second wildcard position, m, and a colon. The two middle positions may contain different characters or the same character. Because each period represents exactly one character, the pattern does not accept a missing middle character or an extra character in either wildcard position.
Testing F..m:
Determine whether the listed strings fit the fixed structure F..m:.
- 1Read the fixed positions
The string must begin with F and finish with m:.
- 2Read the wildcard positions
The two positions between F and m: may contain any two characters.
- 3Compare the candidates
From:, Fxxm:, F12m:, and F!@m: each has F, two middle characters, and m: in the required positions.
All four candidates fit the pattern F..m:.
The One-Character Limit
The period is flexible about what character it accepts, but it is not flexible about how many positions it consumes. One period matches one character. Two periods match two characters. A period does not stand for an arbitrary-length portion of text.
Searching Lines with Python
Python's re module provides re.search() for finding a pattern within a string. When given a pattern containing 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 lines whose text contains a known structure with variable characters.
import re
lines = ["From:", "F12m:", "Notice", "F!@m:"]
for line in lines:
if re.search("^F..m:", line):
print(line)- 1The pattern begins with ^, so the required F must occur at the beginning of the line.
- 2The two periods each accept one character.
- 3The final m: must follow those two characters.
- 4
re.search()checks each line and returns a match object when the pattern is found. - 5The if statement prints only lines for which a match object is returned.
- The re module supplies
re.search(). - The caret anchors the pattern to the beginning of the line.
- F..m: requires F, two single-character positions, m, and a colon.
- Only lines containing the required structure at the beginning are printed.
From:
F12m:
F!@m:Beginning Anchors and Literal Periods
The caret, written as ^ in the pattern ^F..m:, anchors the pattern to the beginning of the line. The pattern therefore requires F to be the first character before the two wildcard positions and the final m:. Without that beginning anchor, re.search() can find the matching structure later within a longer string.
The period has a different meaning when you write the regular expression. An unescaped period is a wildcard. To search for an actual period character in the text, escape it as backslash-period, written in regex notation as \. , or place it in a character class as [.]. This distinction matters for text such as email addresses, file names, and sentences.
| Pattern notation | Meaning | Possible target |
|---|---|---|
| . | One character of any kind | a |
| \. | A literal period character | . |
| [.] | A literal period character inside a character class | . |
The same visible period symbol can represent a wildcard or a literal character depending on the pattern notation.
Mistakes in Period Patterns
Common mistake Assuming one period can match several characters
What it looks likeUsing F.m: when the target has two variable characters between F and m:
Why it failsThe pattern contains only one wildcard position, so it allows only one character there.
Do this insteadUse F..m: when two character positions must be variable.
Common mistake Expecting a period to match zero characters
What it looks likeUsing F..m: to match Fm:Why it failsEach period requires one character position.
Do this insteadRemember that the period matches exactly one character, not zero characters.
Common mistake Using an unescaped period to find a literal period
What it looks likeUsing . in a pattern intended to find the period in a file name or sentence
Why it failsAn unescaped period acts as a wildcard and can match any single character.
Do this insteadUse \. or [.] when the target text must contain an actual period.
Common mistake Forgetting the beginning anchor
What it looks likeUsing F..m: when the required structure must begin the line
Why it failsre.search()can find a matching pattern within a string, while ^F..m: requires the pattern at the beginning.Do this insteadAdd ^ when the line must start with the required structure.
Pattern-Building Practice
Create a regular expression that matches a five-character structure beginning with F, containing any two middle characters, and ending with m:. Then decide whether the pattern should begin with ^ if the structure must occur at the start of each line.
Hints
- Write the fixed characters first.
- Use one period for each variable character position.
- Add the caret when the first required character must be the first character of the line.
Suppose you need to find an actual period character in text. Choose between ., \. , and [.], and explain why the other notation would not express the same requirement.
Hints
- An unescaped period is a wildcard.
- The escaped form and the character-class form represent a literal period.
Key Takeaways
- In a regular expression, each period matches exactly one character of any kind.
- Multiple periods are required for multiple variable character positions.
- The pattern F..m: matches a five-character structure beginning with F and ending with m:.
- Python
re.search()finds a pattern within a string and can be used to filter lines. - Use ^ to require a match at the beginning of a line, and use \. or [.] to match an actual period character.