Regular Expressions
Faryal Shamsi
Lecturer Computer Science
Sukkur IBA University
What is a Regular Expression
• An expression that describes a language
• A sequence of symbols and characters expressing a string, a pattern
or a language
2
Formal Definition of RE
Algebra for Languages
Previously we discussed these operators:
• Union
• Concatenation
• Kleene Star
Example of Regular Expression
• (a+b.c)* is a regular expression
• That describes the following language:
a, bc* , a, bc, aa, abc, bca,...
Example
A regular expression: a b c * (c )
Not a regular expression: a b
Languages of Regular Expressions
• Lr : language of regular expression r
• Example
L(a b c) * , a, bc, aa, abc, bca,...
• For primitive regular expressions:
L
L
La a
• For regular expressions r1 and r2
Lr1 r2 Lr1 Lr2
Lr1 r2 Lr1 Lr2
Lr1 * Lr1 *
Lr1 Lr1
Example 1
a b a *
La b a * La b La *
La b La *
La Lb La *
a b a*
a, b , a, aa, aaa,...
a, aa, aaa,..., b, ba, baa,...
Example 2
• Regular expression r a b * a bb
Lr a, bb, aa, abb, ba, bbb,...
Example 3
• Regular expression r aa * bb * b
Lr {a b
2n 2m
b : n, m 0}
Example 4
• Regular expression r (0 1) * 00 (0 1) *
L(r ) = { all strings with at least
two consecutive 0 }
Example 5
• Regular expression r (1 01) * (0 )
L(r ) = { all strings without
two consecutive 0 }
RE Examples
• L(001) = {001}
• L(0+10*) = { 0, 1, 10, 100, 1000, 10000, … }
• L(0*10*) = {1, 01, 10, 010, 0010, …} i.e. {w | w has exactly a single 1}
• L()* = {w | w is a string of even length}
• L((0(0+1))*) = { ε, 00, 01, 0000, 0001, 0100, 0101, …}
• L((0+ε)(1+ ε)) = {ε, 0, 1, 01}
• L(1Ø) = Ø ; concatenating the empty set to any set yields the empty set.
• Rε = R
• R+Ø = R
• Note that R+ε may or may not equal R (we are adding ε to the language)
• Note that RØ will only equal R if R itself is the empty set.
Equivalent Regular Expressions
• Regular expressions r1 and r2
• are equivalent if L(r1 ) L(r2 )
Example
L = { all strings without
two consecutive 0 }
r1 (1 01) * (0 )
r2 (1* 011*) * (0 ) 1* (0 )
r1 and r2
L(r1 ) L(r2 ) L
are equivalent
regular expr.