Regular Expressions
(Content adapted from Critchlow & Eck)
Though we have used the term string throughout to refer to a sequence of symbols from an alphabet, an alternative term that is frequently used is word. The analogy seems fairly obvious: strings are made up of "letters" from an alphabet, just as words are in human languages like English. In English, however, there are no strict rules specifying which sequences of letters can be used to form legal English words—even unlikely combinations like ghth and ckstr have their place. While some formal languages may simply be random collections of arbitrary strings, more interesting languages are those where the strings in the language all share some common structure: ; ; . In all of these languages, there are structural rules which determine which sequences of symbols are in the language and which aren't. So despite the terminology of "alphabet" and "word" in formal language theory, the concepts don't necessarily match "alphabet" and "word" for human languages. A better parallel is to think of the alphabet in a formal language as corresponding to the words in a human language; the words in a formal language correspond to the sentences in a human language, as there are rules (grammar rules) which determine how they can legally be constructed.
One way of describing the grammatical structure of the strings in a language is to use a mathematical formalism called a regular expression. A regular expression is a pattern that "matches" strings that have a particular form. For example, consider the language (over alphabet ) . What is the symbol-by-symbol structure of strings in this language? Well, they start with an , followed by zero or more 's or 's or both, followed by an . The regular expression is a pattern that captures this structure and matches any string in ( and have their usual meanings, and designates or.1) Conversely, consider the regular expression (. This is a pattern that matches any string that either has the form " followed by zero or more 's or 's or both" (i.e., any string that starts with an ) or has the form "zero or more 's or 's or both followed by an " (i.e. any string that ends with an ). Thus the regular expression generates the language of all strings that start or end (or both) in an : this is the set of strings that match the regular expression.
Here are the formal definitions of a regular expression and the language generated by a regular expression:
Let be an alphabet. Then the following patterns are regular expressions over :
- and are regular expressions;
- is a regular expression, for each ;
- if and are regular expressions, then so are , , and (and of course, and ). As in concatenation of strings, the is often left out of the second expression. (Note: the order of precedence of operators, from lowest to highest, is , , .)
No other patterns are regular expressions.
The language generated by a regular expression , denoted , is defined as follows:
- , i.e., no strings match ;
- , i.e., matches only the empty string;
- , i.e., matches only the string ;
- , i.e., matches strings that match or or both;
- , i.e., matches strings of the form "something that matches followed by something that matches ";
- , i.e., matches sequences of 0 or more strings each of which matches .
- , i.e., matches exactly those strings matched by .
Example: Let , and consider the regular expression . What is ? Well, so , and is the set of all strings of zero or more 's, so . Similarly, . Since , we have , which is the set of all strings of the form "zero or more 's followed by zero or more 's".
Example: Let , and consider the regular expression . Since , . Similarly, and . Now , and (the last equality is from clause 7 of the efinition), and . So is the set of strings formed by concatenating or or with zero or more pairs of 's.
A language is regular if it is generated by a regular expression.
Regular languages, then, are languages whose strings' structure can be described in a very formal, mathematical way. The fact that a language can be "mechanically" described or generated means that we are likely to be able to get a computer to recognize strings in that language. We will pursue the question of mechanical language recognition in a later section, and subsequently will see that our first attempt to model mechanical language recognition does in fact produce a family of "machines" that recognize exactly the regular languages. But first, in the next section, we will look at some practical applications of regular expressions.
Exercises
Give English-language descriptions of the languages generated by the following regular expressions:
Answer
Strings of 's and 's.
Answer
Strings of all 's or all 's (no mixing).
Answer
Strings with an even number of 's (and any number of 's).
Answer
Strings of 's and 's where every is followed by a .
Give regular expressions over that generate the following languages:
Answer
Answer
Answer
Answer
Answer
Answer
Prove that all finite languages are regular.
Answer
If the language is , then it can be given by the regular expression .
- Various symbols have been used to represent the "or" operation in regular expressions. Both and have been used for this purpose. In this book, we use the symbol because it is commonly used in computer implementations of regular expressions.↩