What is a pattern and how do we match it to text? This article hopes to answer these questions gradually. By a string we mean a sequence, possibly empty, of characters from some alphabet. For simplicity's sake we suppose that we are dealing with ASCII characters, so each of the 256 characters is represented by one byte of storage. The text will be given by a string. At least for one's imagination one may think of an array text in memory. We use the notation #text for the number of characters in the array..

Our patterns, whatever they turn out to be, will be anchored to the start of the text. So the operation of matching will start at the beginning of the text. We think of the operation as scanning through the text, using the offset i from the beginning as a pointer. If i is never decreased then we say that no backtracking is involved. The matching operation can be thought of as a function which returns up to two values

  • a boolean value: true for success, false for failure
  • in the case of success, an updated index referring to the first character not matched

            success, next_text = match (pattern, text)
It is time to look at some simple examples of patterns.