# Regular Expressions: Examples

Abstract

Some sample problems relating to creating and manipulating regular expressions.

# 1 JFLAP

1. Read the Regular Expressions section of the
JFLAP Tutorial.

2. JFLAP does not feature a direct way to check regular expressions against strings. You can do it indirectly by asking JFLAP to convert the regular expression to and NFA, and then running that NFA.

It may be easier, however, to check your regular expressions via ordinary Linux commands. You may recall that grep is a program that tests lines of text against a regular expression, echoing those lines that match. We can convert the textbook & JFLAP style regular expressions to Linux regular expressions pretty easily:

• Linux uses | for the union operator instead of +.
• JFLAP uses ! for the $\epsilon$ character. In Linux/grep, we can use () instead.
• grep matches any string containing the desired pattern. If we want grep to match only if the entire string matches the regular expression, we use the -x option.

So if we wanted to check various strings against the regular expression $(a + b)^*$, we could give the commands

echo a | grep -E -x '(a|b)*'
echo aa | grep -E -x '(a|b)*'
echo aab | grep -E -x '(a|b)*'
⋮


(You might recall that Linux has keyboard shortcuts to recall recently entered commands, allowing you to make small changes to a prior command instead of retyping the whole thing.)

This command will either print our string if it matches or will print nothing if it does not. (The ‘-E’ selects the “extended” form of grep that makes full regular expression processing available.)

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

• a
• b
• aaaba
• aaaca

You can enter $\epsilon$ (the empty string) for testing by using two quotes with nothing between them ('' or ""). Try testing the empty string against these regular expressions:

• ‘(a|b)*’
• ‘(a|b)’

There is a difference in the output, subtle though it may be.

# 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 or with 110.

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:

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

Now that you’ve seen the method, can you fill in the rest of table?

Reveal