Regular Expressions

COS2601 - Theoretical Computer Science II · Automata Theory

Regular Expressions

Regular expressions are a powerful way to describe languages in formal language theory. They allow us to define sets of strings using specific symbols and operators. In this section, we will explore the basic components of regular expressions, how they are constructed, and their significance in defining languages.

Basic Components of Regular Expressions

A regular expression is built from the following components:

  • Alphabet: A set of symbols from which strings can be formed. For example, if our alphabet is {a, b}, we can form strings like 'a', 'b', 'aa', 'ab', etc.
  • Null String (A): This represents the empty string, which has no characters.
  • Concatenation: If r1 and r2 are regular expressions, then the expression r1r2 represents the concatenation of the strings defined by r1 and r2. For example, if r1 = 'a' and r2 = 'b', then r1r2 = 'ab'.
  • Union (+): The expression r1 + r2 denotes a choice between the strings defined by r1 and r2. For example, if r1 = 'a' and r2 = 'b', then r1 + r2 represents the set {'a', 'b'}.
  • Kleene Star (*): If r is a regular expression, then r* denotes zero or more concatenations of the strings defined by r. For example, if r = 'a', then r* = {'', 'a', 'aa', 'aaa', ...}.

Constructing Regular Expressions

Regular expressions can be constructed according to specific rules. Here are the rules for creating regular expressions:

  1. Every letter of the alphabet can be a regular expression. The null string (A) is also a regular expression.
  2. If r1 and r2 are regular expressions, then the following are also regular expressions:
    • (r1) - Parentheses can be used to group expressions.
    • r1r2 - Concatenation of two regular expressions.
    • r1 + r2 - Union of two regular expressions.
    • r1* - Kleene star applied to a regular expression.
  3. Nothing else is considered a regular expression.

Examples of Regular Expressions

Let's look at some examples of regular expressions and the languages they define:

Example 1: Simple Strings

Consider the regular expression 'a'. This expression defines the language L1 = {a}. It includes only the single string 'a'.

Example 2: Concatenation

The regular expression 'ab' defines the language L2 = {ab}. This language includes only the string 'ab'.

Example 3: Union

The regular expression 'a + b' defines the language L3 = {a, b}. This language includes the strings 'a' and 'b'.

Example 4: Kleene Star

The regular expression 'a*' defines the language L4 = {A, a, aa, aaa, ...}. This language includes the empty string and any number of 'a' characters.

Example 5: Combining Operators

The regular expression '(a + b)*' defines the language L5 = {A, a, b, aa, ab, ba, bb, aaa, aab, aba, abb, ...}. This language includes all possible strings formed from 'a' and 'b', including the empty string.

Defining Specific Languages

Regular expressions can be used to define more complex languages. Let's look at some specific examples:

Example 6: Strings with At Least Two 'a's

The regular expression 'b*a(a + b)*a(a + b)*' defines the language L6, which includes strings that have at least two 'a's. This can be illustrated by the following strings: 'aa', 'aba', 'aab', 'baaa', etc.

Example 7: Strings with Exactly Two 'a's

The regular expression 'b*ab*ab*' defines the language L7, which includes strings that have exactly two 'a's. This can include strings like 'aab', 'aba', 'baa', and 'abab'.

Example 8: Strings with At Least One 'a' and One 'b'

The regular expression '(a + b)*a(a + b)*b(a + b)* + (a + b)*b(a + b)*a(a + b)*' defines the language L8, which includes strings that contain at least one 'a' and at least one 'b'. Examples of valid strings include 'ab', 'ba', 'aab', 'bba', etc.

Formal Definition of Regular Expressions

As mentioned earlier, the set of regular expressions is defined recursively. Each regular expression corresponds to a specific language. The rules for associating a language with regular expressions are as follows:

  1. The language associated with the regular expression that is just a single letter is that one-letter word alone, and the language associated with A is just {A}.
  2. If r1 is a regular expression associated with the language L1 and r2 is a regular expression associated with the language L2, then:
    • The regular expression (r1)(r2) is associated with the product L1L2.
    • The regular expression r1 + r2 is associated with the union L1 + L2.
    • The language associated with the regular expression (r1)* is L1*.
  3. Every regular expression must follow these rules to be valid.

Common Mistakes with Regular Expressions

Watch out: A common mistake is to confuse the use of the Kleene star (*) with standard algebraic exponentiation. Remember that r* represents zero or more occurrences of r, while r^n represents exactly n occurrences.

Summary

  • Regular expressions are used to define languages in formal language theory.
  • They consist of an alphabet, null string, concatenation, union, and Kleene star.
  • Regular expressions can be constructed using specific rules.
  • They can describe simple and complex languages.

Check your understanding

  1. What does the regular expression 'a*b*' define?
  2. How would you express a language that contains all strings of 'a's followed by 'b's?
  3. What is the significance of the Kleene star in regular expressions?
  4. Provide an example of a regular expression that defines a language with at least one 'a' and one 'b'.