Concepts / Anchors: Matching at the Start or End of a Line

Anchors: Matching at the Start or End of a Line

The period (.) in a regular expression is a wildcard that matches any single character—letters, digits, spaces, punctuation, or any other character.

  • Programming

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?

  • Any string containing F and m in any positions
  • A five-character string beginning with F and ending with m:
  • A string beginning with F and containing exactly two periods
Reveal answer

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.

next positionnext positionnext positionnext positionFfixed character.one character.one charactermfixed character:fixed character
How do the fixed characters and wildcard positions combine to form a five-character match?

Testing F..m:

Determine whether the listed strings fit the fixed structure F..m:.

Read the fixed positions: The string must begin with F and finish with m:.

Read the wildcard positions: The two positions between F and m: may contain any two characters.

Compare 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.

fitsdoes not fitdoes not fitF..m:two wildcard positionsF12m:one character in eachpositionFm:no characters for twopositionsF123m:three characters for twopositions
How does the period wildcard behave when the target has zero, one, or multiple characters for that position?

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)

Output
From:
F12m:
F!@m:
each linepattern foundpattern not foundInput linesFrom:, F12m:, Notice, F!@m:re.search()^F..m:Accepted linesFrom:, F12m:, F!@m:Rejected lineNotice
How does each input line move through re.search() when the pattern contains an anchor and period wildcards?

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.

matchesmatches.any one characteraone possible match\.literal period.actual period character
What does each pattern match in the searched text?
Pattern notationMeaningPossible target
.One character of any kinda
\.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

  • Assuming one period can match several characters

    The pattern contains only one wildcard position, so it allows only one character there.

    Fix: Use F..m: when two character positions must be variable.

  • Expecting a period to match zero characters

    Each period requires one character position.

    Fix: Remember that the period matches exactly one character, not zero characters.

  • Using an unescaped period to find a literal period

    An unescaped period acts as a wildcard and can match any single character.

    Fix: Use \. or [.] when the target text must contain an actual period.

  • Forgetting the beginning anchor

    re.search() can find a matching pattern within a string, while ^F..m: requires the pattern at the beginning.

    Fix: Add ^ when the line must start with the required structure.

Pattern-Building Practice

EASY

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.
MEDIUM

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.