Concepts / Regular Expression Pattern Components

Regular Expression Pattern Components

A plain pattern is found only when its exact characters occur in order in the stream. Each character of the pattern must match the stream's characters one after another, with nothing rearranged and nothing skipped.

  • Programming

Finding a Word Inside a Longer Text

Suppose you have a long run of text and you want to know whether a particular piece of it appears somewhere inside. For a short note you can simply read the whole thing by eye. That approach becomes impractical for a log file, a long document, or a stream of incoming input. A regular expression gives you a way to describe the piece you are looking for and then ask a program to locate it.

What do you think happens?

Before reading on: the pattern is the plain word cat, and the stream is the text the cat sat. What does the search report?

  • It reports that cat occurs in the stream

  • It reports that cat does not occur in the stream

  • It reports the whole stream back to you

Reveal answer
Answer:

It reports that cat occurs in the stream.

The plain pattern cat has its exact characters c, a, t occurring in order inside the stream, so the search finds it. The basic outcome of a search is whether the pattern was found.

The Pattern and the Stream

The source introduces this topic under the heading of searching and makes one central claim: searching a stream for a pattern is the simplest use of a pattern. Everything else in regular expression work builds on that single idea, so it is worth understanding it slowly before adding any complexity.

Definition

A pattern is what you seek. A stream is where you seek it. A search brings the two together: the pattern is applied to the stream, and the question being answered is whether the pattern can be found in it.

The two words play different roles, and it is easy to mix them up. The pattern is the thing you are looking for. The stream is the text you look through. Searching is not the same as the stream being checked as a whole against the pattern. The pattern is compared against the text, and the result tells you about the pattern's presence within the text.

Key point

A plain pattern is found only when its exact characters occur in order in the stream. Each character of the pattern must match the stream's characters one after another, with nothing rearranged and nothing skipped.

Reading the Stream Step by Step

The word stream suggests text that is read in order rather than inspected all at once. For a beginner, it helps to picture the stream as a line of characters with a reader moving steadily along it. The source does not describe how that reading is implemented, so treat this picture as a way to hold the idea in mind rather than as a claim about any particular search engine.

Using that picture, the search for cat in the text the cat sat can be traced as a sequence of attempts. At each position, the pattern is lined up against the stream starting there. If the characters do not match, the attempt fails and the reader moves forward to the next position. The diagram below traces this progression.

Position at tt does not match cPosition at hh does not match cPosition at ee does not match cPosition at spacespace does not match cPosition at cc, a, t match in orderPattern foundcat locatedmove forwardmove forwardmove forwardmove forwardall characters matched
At each position, which character of the stream is compared with the pattern, and where does the scan move next after a mismatch?

Notice that the early attempts fail quickly. The stream's t, h, e, and space are each compared with the first character of the pattern, c, and none of them match, so the reader keeps moving. Only at the c of cat do the pattern's characters begin to line up with the stream, and the search succeeds once all three characters have occurred in order.

Tracing a Search With No Match

Worked example

Searching for dog in the text the cat sat

Does the plain pattern dog occur in the stream the cat sat?

  1. 1
    Set up the pattern and the stream

    The pattern is dog and the stream is the cat sat. The search asks whether d, o, g occur in order somewhere in the stream.

  2. 2
    Check each starting position

    Each position of the stream is tried. At t, h, e, space, c, a, and t, the stream's character does not match the d that the pattern requires first, so no attempt gets past the first character.

  3. 3
    Recognize that no position succeeded

    Since no starting position produced the exact characters d, o, g in order, the pattern never appears in the stream.

  4. 4
    Report the outcome

    The search ends with the answer that the pattern was not found. Its basic outcome is simply absence.

Result

No match is found. The search has shown that dog is absent from the stream.

A no-match result is a real and complete answer, not a failure of the search. Once every position has been tried without success, the search is finished, and the correct conclusion is that the pattern does not occur in the stream. The central question of a search is always yes or no: does the pattern occur in the stream?

What a Search Reports

The source describes the most basic result of a search as whether the pattern was found in the stream. When the pattern occurs, the search has located it. When it does not, the search has shown that the pattern is absent. Keeping this framing in mind helps you read the output of any search correctly, because the most basic result is simply whether a match was found.

Mistakes Beginners Make

  • Common mistake

    Treating the pattern as a question about the whole stream

    What it looks like

    Assuming that searching for cat in the cat sat means the entire stream must be the word cat.

    Why it fails

    The search asks whether the pattern occurs somewhere inside the stream, not whether the stream equals the pattern.

    Do this instead

    Read the question as: can the pattern be found inside this stream?

  • Common mistake

    Expecting characters to match out of order

    What it looks like

    Believing that the pattern cat will be found in a stream containing a, t, c in that scrambled order.

    Why it fails

    A plain pattern is found only when its exact characters occur in order in the stream.

    Do this instead

    Check that the characters appear consecutively and in the same sequence as the pattern.

  • Common mistake

    Reading a no-match result as an error

    What it looks like

    Seeing that dog was not found in the cat sat and concluding the search broke.

    Why it fails

    Absence is one of the two valid outcomes of a search; the search has answered the question.

    Do this instead

    Interpret a no-match result as the correct answer: the pattern is absent from the stream.

  • Common mistake

    Forgetting that the stream is read in order

Common assumptionWhat the source supports
A search checks whether the stream is the patternA search checks whether the pattern occurs in the stream
Characters can be found in any orderA plain pattern is found only when its exact characters occur in order
No match means the search failedNo match is a valid outcome showing the pattern is absent
The result is more than found or not foundThe basic outcome is whether the pattern was found

Misunderstandings about what a search does and what it returns

Practice Tracing a Search

EASY

Stream: the dog ran. Pattern: dog. Before you check, predict whether the search finds the pattern. Then trace the attempts position by position and state the final outcome in one sentence.

Hints
  • Look for the exact characters d, o, g occurring in order.
  • The stream begins with t, h, e, space, then d, o, g, so one position should line up completely.
  • State the outcome as found or not found, since that is the basic result of a search.

Key Takeaways

  • Searching a stream for a pattern is the simplest use of a pattern: the pattern is what you seek, and the stream is where you seek it.
  • A plain pattern is found only when its exact characters occur in order in the stream.
  • The basic outcome of a search is whether the pattern was found; a no-match result is a complete and valid answer.
  • Do not assume a search checks the whole stream against the pattern, and do not expect characters to match out of order.

Related concepts

More concepts starting with R