Concepts / Quantifiers: Matching Multiple Characters

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.

  • Programming

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?

  • It matches because the two periods cover two characters.

  • It does not match because a period only matches punctuation.

  • It matches only if the middle characters are digits.

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.

FliteralFrom:five characters.one characterFom:four characters.one characterFxxm::six charactersmliteral:literalnext positionnext positionnext positionnext positionfitstoo few positionsextra position
How does the pattern F..m: consume exactly one character at each period position, and what happens when the candidate has too few or too many characters for the pattern?

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.
cliteral ccata is accepted.one characterc9t9 is acceptedtliteral tc tspace is acceptednext positionnext positionfitsfitsfits
How do literal characters and period wildcards align with variable strings such as cat, c9t, and c t?
Worked example

Reading F..m:

Determine which of From:, Fxxm:, F12m:, and F!@m: fit the pattern F..m:.

  1. 1
    Count the pattern positions

    The pattern contains five positions: F, one period position, a second period position, m, and a colon.

  2. 2
    Check the fixed positions

    Each candidate begins with F and ends with m:.

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

Result

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.

Fposition 1Frposition 2Froposition 3Fromposition 4From:position 5one characterone characterliteral mliteral colon
How does a pattern containing several periods match a sequence of individual characters rather than one multi-character wildcard?

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.

Code walkthrough
import re

lines = ["From:", "F12m:", "Subject: Notes", "Format:"]

for line in lines:
    if re.search(r"^F..m:", line):
        print(line)
  1. 1
    The re module supplies the search function.
  2. 2
    The pattern ^F..m: requires the match to begin at the start of the string.
  3. 3
    From:, F12m:, and Format: fit the required structure.
  4. 4
    Subject: Notes does not begin with F, so it is not printed.
  • The caret anchors the pattern to the beginning of the line.
  • F must appear first, followed by two characters matched by the periods.
  • The text must then contain m followed by a colon.
  • re.search() tests each line, and matching lines are printed.
Output
From:
F12m:
Format:
Output
From:
F12m:
Format:
Text lineFrom:re.search()pattern testMatch objectline printed^F..m:fixed structureNo matchline not printedinputpatternfoundnot found
How does text flow into re.search(), become a match or non-match result, and determine whether a line is printed?

Wildcard or Literal Period

Pattern formMeaningAccepted character
.Wildcard periodAny single character
\.Escaped periodThe punctuation character .
[.]Period inside a character classThe punctuation character .

Use an escaped period or a period in a character class when the text must contain an actual period.

.any charactera, 7, spaceaccepted values\.period character.accepted valuematchesmatches
What is the difference between a period that matches any single character and an escaped period that matches only the punctuation character?

Mistakes with Period Wildcards

  • Common mistake

    Treating one period as a match for several characters.

    What it looks like

    Using . when the intended pattern needs to cover two character positions.

    Why it fails

    Each period represents exactly one character position.

    Do this instead

    Use multiple periods when multiple individual character positions must be matched.

  • Common mistake

    Expecting a plain period in a pattern to match only punctuation.

    What it looks like

    Using . when the target text must contain the actual character .

    Why it fails

    An unescaped period acts as a wildcard.

    Do this instead

    Use \. or [.] to match a literal period.

  • Common mistake

    Forgetting that fixed characters still have to appear in the right positions.

    What it looks like

    Testing a string that does not start with F against F..m:.

    Why it fails

    The periods are flexible, but F, m, and the colon remain required by the pattern.

    Do this instead

    Check both the wildcard positions and every literal character in the pattern.

  • Common mistake

    Using a pattern without the beginning anchor when the structure must start at the beginning of a line.

    What it looks like

    Using F..m: instead of ^F..m: when filtering lines that must begin with F.

    Why it fails

    The source pattern uses ^ to anchor the match to the beginning of the line.

    Do this instead

    Include ^ when the required structure must occur at the start.

Apply the Pattern

MEDIUM

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

  1. 1
    A period in a regular expression matches exactly one character of any kind.
  2. 2
    Several periods match several separate character positions; one period does not represent an entire multi-character string.
  3. 3
    Patterns such as F..m: combine fixed characters with flexible wildcard positions.
  4. 4
    Python re.search() scans a string for a pattern and returns a match object when the pattern is found.
  5. 5
    Use \. or [.] when the text must contain a literal period character.

At a glance

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

Related concepts

More concepts starting with Q