Regular Expressions: Examples

CS390, Spring 2024

Last modified: Feb 24, 2023
Contents:

Abstract

Some sample problems relating to creating and manipulating regular expressions.

1 Regular Expressions and Automat

  1. Read about Regular Expressions in the Automat Editor.

  2. Create the following regular expression: (a+b)*

    Try testing that regular expression with each of the following strings:

    • (the empty string)
    • a
    • b
    • aaaba
    • aaaca
  3. Remove the closure (*) from your regular expression. On how many of the above inputs would you expect the accept/reject behavior to change? Test the modified regular expression on those inputs. Any surprises?

2 Sample Problems

2.1 Basic Patterns

  1. Write a regular expression to describe the set of strings with one element, the string 101.

    Reveal
  2. Prove: No regular expression that does not use closure (*) can accept a string longer than itself.

    Reveal
  3. Write regular expressions to describe the set of strings that

    • begin with 101
    • end with 101
    • contain 101
    Reveal

2.2 Combining Smaller Regular Expressions into Larger Ones

  1. Write a regular expression to describe the set of strings that begin with 101 and end with 101.

  2. Write a regular expression to describe the set of strings that consist of zero or more non-overlapping repetitions of 101.

    Reveal

2.3 Converting Finite Automata to Regular Expressions

  1. Write a regular expression accepting the same language as this automaton:

     

    Before we actually dive into this problem, let me highlight some simplification rules that will recur in this problem:

    • For any regular expression R, $\epsilon R = R \epsilon = R$.
    • For any regular expression R, $\emptyset R = R \emptyset = \emptyset$.
    • For any regular expression R, $\emptyset + R = R$.
    • For any regular expression R, $(\epsilon + R)^* = R^*$.
    • For any regular expression R, $(\epsilon + R)R^* = R^*(\epsilon + R)= R^*$.
    • For any regular expressions R and S, $R + RS^* = RS^*$
    • For any regular expressions R and S, $R + S^*R = S^*R$

    Can you write the first “iteration” of the table, containing $R^{(0)}$?

    Now that you’ve seen the method, can you fill in the table for $R^{(1)}$?

    Reveal

    Can you fill in the table for $R^{(2)}$?

    Reveal

    Can you fill in the table for $R^{(3)}$?

    Reveal

    Can you fill in the table for $R^{(4)}$?

    Reveal

    Now, the regular expressions here are pretty ugly. I don’t consider this to be a practical algorithm.

    Its important is that it establishes that, for any FA, we can generate a regular expression.

    We have earlier established that, for any regular expression, we can generate an NFA.

    Combining those two statements, we realize that the set of languages that can be described by regular expressions is exactly the same set of languages tha can be described by finite automata.