Concepts / Matching Patterns in Streams

Matching Patterns in Streams

A plain pattern does not match a loose resemblance. The characters must be exactly the same, and they must occur in the same order.

  • Programming

Finding a Piece of Long 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 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.

The simplest use of a pattern is to search for it in a stream. Everything else in regular expression work builds on this one idea, so it is worth understanding slowly before adding any complexity. Keep the example below in mind as you read: a pattern is a plain word, and the stream contains that word somewhere in the middle.

What do you think happens?

The pattern is the word cat, and the stream is the text 'the cat sat'. What does the search report?

  • The exact position where cat starts, and nothing else

  • Whether the pattern was found in the stream

  • A copy of the stream with cat removed

Reveal answer
Answer:

Whether the pattern was found in the stream. Here the answer is yes, because the characters c, a, t occur in order inside the stream.

The most basic outcome of a search is a yes or no answer to the question of whether the pattern occurs in the stream. Telling you where it sits is a further detail that depends on what the search is asked to return.

Pattern and Stream: Two Roles

Definition

A pattern is what you seek. A stream is where you seek it. A search applies the pattern to the stream, and the question it answers is whether the pattern can be found in it.

The two words in this topic play different roles, and mixing them up makes every later result harder to read. The pattern is the thing you are looking for. The stream is the text in which you look for it. The search is the act that brings the two together. A search does not ask what the stream contains in general; it asks a narrower question about one specific pattern.

Reading the Stream Piece by Piece

The word stream suggests text that is read in order rather than inspected all at once. A helpful picture is a line of characters with a reader moving steadily along it, one character after another. The source does not explain how this reading is implemented in any particular engine, so treat this picture as a way to hold the idea in mind rather than as a description of the internal machinery.

Follow the reader through the stream 'the cat sat' while searching for the pattern cat. Each step shows one character that has just been read and what it is being compared with. The first few characters do not match the first character of the pattern, so the reader keeps moving. When the reader reaches the c of cat, the comparison begins to succeed, and the next characters must continue to agree with the pattern in order.

Read tcompare with c: noRead hcompare with c: noRead ecompare with c: noRead spacecompare with c: noRead ccompare with c: yesRead acompare with a: yesRead tcompare with t: yes, foundnext characternext characternext characternext characternext characternext character
At each step of the stream, which character has been read, and what it is being compared with, while searching for the pattern cat.

The diagram is a picture of one progression through the stream, not a claim about how every search works. The key idea it carries is that the pattern is compared against the text as the text is read, and the search moves forward along the stream as it goes.

Matching a Plain Word Exactly

The simplest form of a regular expression search uses a plain pattern, which is a sequence of literal characters. A plain pattern is found only when its exact characters occur in order in the stream. Each character of the pattern must agree with a character of the stream, and the agreeing characters must follow one another without anything else being required by the pattern.

Worked example

Searching for a plain word in a sentence

Pattern: cat. Stream: the cat sat. Is the pattern found in the stream?

  1. 1
    Identify the pattern characters

    The pattern has three characters in order: c, then a, then t.

  2. 2
    Read the stream in order

    The stream is t, h, e, space, c, a, t, space, s, a, t. The reader moves through it one character at a time.

  3. 3
    Look for the first pattern character

    The reader passes t, h, e, and space without a match, then reaches c.

  4. 4
    Check that the rest follows in order

    The next stream character is a and the one after it is t, so the pattern characters occur in order.

  5. 5
    Report the outcome

    The pattern occurs in the stream, so the search reports that it was found.

Result

Found. The exact characters c, a, t occur in order inside the stream.

Key point

A plain pattern does not match a loose resemblance. The characters must be exactly the same, and they must occur in the same order.

Where a Match Sits in the Stream

Once a match is found, it occupies particular positions in the stream. Counting from zero, the stream 'the cat sat' has the character t at position 0, h at position 1, e at position 2, a space at position 3, c at position 4, a at position 5, t at position 6, and so on. The pattern cat therefore occupies positions 4 through 6. The diagram lines up each stream character with its position so you can see which positions the match covers.

tposition 0hposition 1eposition 2spaceposition 3cposition 4aposition 5tposition 6spaceposition 7sposition 8aposition 9tposition 10
Which positions in the stream the match for the pattern cat occupies, and how they line up with the three characters of the pattern.

Notice that the t at position 10 is also a t, but it does not complete the pattern because the characters before it are not c and a in order. A match is about the whole pattern occurring as a block of characters in order, not about any single character agreeing.

When the Search Finds No Match

Now try a pattern that the stream does not contain. Searching the same stream 'the cat sat' for dog produces no match. The reader moves to the end of the stream without finding d, o, g in order. At that point the search has shown that the pattern is absent. This is a complete and valid answer to the question the search asks, and it is the same kind of outcome as a success, just with the opposite value.

Start searchRead next characterPattern found here?Characters left?Report foundReport not foundyesnoyesno
The path a search takes through the stream, showing where it reports a match and where it reports that the pattern is absent.

The flowchart is a simplified picture of the two possible endings. Either the pattern is located, or the stream runs out with no match. Both endings produce a result, and the no-match result is the one that tells you the pattern is absent from the stream.

Misreading What a Search Returns

  • Common mistake

    Expecting a loose or partial resemblance to count as a match

    What it looks like

    Pattern cat in the stream 'c a t' with spaces between the letters.

    Why it fails

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

    Do this instead

    Check that the characters themselves appear, unbroken and in order, before concluding the pattern was found.

  • Common mistake

    Thinking the most basic result is a full description of the match

    What it looks like

    Expecting a search for cat to hand back the matched text and its surroundings in every case.

    Why it fails

    The basic outcome of a search is whether the pattern was found in the stream. Anything more, such as where the match sits, depends on what the search is asked to report.

    Do this instead

    Start by reading the yes or no answer, then look at any extra details the particular search provides.

  • Common mistake

    Treating a no-match result as a failure to run the search

    What it looks like

    Searching 'the cat sat' for dog and concluding the search did not work.

    Why it fails

    When the pattern does not occur in the stream, the search has successfully shown that it is absent.

    Do this instead

    Read a no-match result as a valid answer: the pattern is not in this stream.

Checking Your Own Searches

EASY

Pattern: sun. Stream: the sunny day. Work through the stream character by character. Is the pattern found, and where does it begin?

Hints
  • Look for the characters s, u, n occurring in order, one after another.
  • The stream is t, h, e, space, s, u, n, n, y, space, d, a, y. Check whether s, u, n appear together in that order.
  • The answer is yes: the pattern is found, and it begins at position 4 when counting from zero.
MEDIUM

Pattern: rain. Stream: the sun shines. Decide whether the search finds the pattern, and say what result the search reports.

Hints
  • Check whether the exact characters r, a, i, n occur in order in the stream.
  • No character sequence r, a, i, n appears in the stream, so the reader reaches the end without a match.
  • The search reports that the pattern is absent from the stream. This is a valid result, not a failure.

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 search reads the stream in order and asks whether the pattern occurs in it; the most basic result is a yes or no answer.
  • A plain pattern is found only when its exact characters occur in order in the stream, with no loose or partial matches.
  • When the pattern does not occur, the search has validly shown that it is absent from the stream.

Related concepts

More concepts starting with M