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.

next positionnext positionnext positionnext positionfitstoo few positionsextra positionFliteralFrom:five characters.one characterFom:four characters.one characterFxxm::six charactersmliteral:literal
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.
next positionnext positionfitsfitsfitscliteral ccata is accepted.one characterc9t9 is acceptedtliteral tc tspace is accepted
How do literal characters and period wildcards align with variable strings such as cat, c9t, and c t?

Reading F..m:

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

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

Check the fixed positions: Each candidate begins with F and ends with m:.

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.

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.

one characterone characterliteral mliteral colonFposition 1Frposition 2Froposition 3Fromposition 4From:position 5
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.

import re lines = ["From:", "F12m:", "Subject: Notes", "Format:"] for line in lines: if re.search(r"^F..m:", line): print(line)

Output
From:
F12m:
Format:
inputpatternfoundnot foundText lineFrom:re.search()pattern testMatch objectline printed^F..m:fixed structureNo matchline not printed
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 .
matchesmatches.any charactera, 7, spaceaccepted values\.period character.accepted value
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

  • Treating one period as a match for several characters.

    Each period represents exactly one character position.

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

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

    An unescaped period acts as a wildcard.

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

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

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

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

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

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

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

Key Takeaways

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