Regular Expressions: Examples
Steven Zeil
Abstract
Some sample problems relating to creating and manipulating regular expressions.
1 JFLAP

Read the Regular Expressions section of the
JFLAP Tutorial. 
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 wantgrep
to match only if the entire string matches the regular expression, we use thex
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 '(ab)*' echo aa  grep E x '(ab)*' echo aab  grep E x '(ab)*' ⋮
(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: ‘(ab)*’
 ‘(ab)’
There is a difference in the output, subtle though it may be.
 Linux uses
2 Sample Problems
2.1 Basic Patterns

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

Prove: No regular expression that does not use closure (
*
) can accept a string longer than itself.Reveal 
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

Write a regular expression to describe the set of strings that begin with
101
or with110
. 
Write a regular expression to describe the set of strings that consist of zero or more nonoverlapping repetitions of
101
.Reveal
2.3 Converting Finite Automata to Regular Expressions
 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