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

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.

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

Ffixed character.one character.one charactermfixed character:fixed characternext positionnext positionnext positionnext position
How do the fixed characters and wildcard positions combine to form a five-character match?
Worked example

Testing F..m:

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

  1. 1
    Read the fixed positions

    The string must begin with F and finish with m:.

  2. 2
    Read the wildcard positions

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

  3. 3
    Compare the candidates

    From:, Fxxm:, F12m:, and F!@m: each has F, two middle characters, and m: in the required positions.

Result

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.

F..m:two wildcard positionsF12m:one character in eachpositionFm:no characters for twopositionsF123m:three characters for twopositionsfitsdoes not fitdoes not fit
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.

Code walkthrough
import re

lines = ["From:", "F12m:", "Notice", "F!@m:"]

for line in lines:
    if re.search("^F..m:", line):
        print(line)
  1. 1
    The pattern begins with ^, so the required F must occur at the beginning of the line.
  2. 2
    The two periods each accept one character.
  3. 3
    The final m: must follow those two characters.
  4. 4
    re.search() checks each line and returns a match object when the pattern is found.
  5. 5
    The 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.
Output
From:
F12m:
F!@m:
Input linesFrom:, F12m:, Notice, F!@m:re.search()^F..m:Accepted linesFrom:, F12m:, F!@m:Rejected lineNoticeeach linepattern foundpattern not found
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.

.any one characteraone possible match\.literal period.actual period charactermatchesmatches
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

  • Common mistake

    Assuming one period can match several characters

    What it looks like

    Using F.m: when the target has two variable characters between F and m:

    Why it fails

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

    Do this instead

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

  • Common mistake

    Expecting a period to match zero characters

    What it looks like
    Using F..m: to match Fm:
    Why it fails

    Each period requires one character position.

    Do this instead

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

  • Common mistake

    Using an unescaped period to find a literal period

    What it looks like

    Using . in a pattern intended to find the period in a file name or sentence

    Why it fails

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

    Do this instead

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

  • Common mistake

    Forgetting the beginning anchor

    What it looks like

    Using F..m: when the required structure must begin the line

    Why it fails

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

    Do this instead

    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.

More concepts starting with A