Regular Expressions Examples 1 PDF
Regular Expressions Examples 1 PDF
Regular Expressions
(Examples)
Regular Expression
Write RE for the following languages for Σ = {a,b}
2
EVEN-EVEN (Σ = {a, b}) i.e. = {Λ, aa, bb, aaaa, aabb, abab,
abba, baab, baba, bbaa, bbbb, …}
RE sets:
R1 = (aa+bb)*
R2 = ((ab+ba)(ab+ba))*
=>
R.E. For EVEN-EVEN =
(aa + bb + (ab + ba )(aa + bb)* (ab + ba))*
Theory of Automata
The set of all strings of a’s and b’s that have atleast two letters, that begin
and end with a’s and that have nothing but b’s inside
ab*a = {aa aba abba abbba abbbba…}
The language that contains all the strings of a’s and b’s in which all the a’s
come before all the b’s
a*b* = {Λ a b aa ab bb aaa aab abb bbb aaa…}
Note:(a*b* is not Equals to (ab)*)
The language of Σ = {a}, defining the odd language
a(aa)*or(aa)*a
The language of Σ = {a}, defining the even language
(aa)*
The language of the strings defined over Σ = {a,b}, which do not contain a
double letter
b(ab)*a + a(ba)*b
Theory of Automata
The language that contains all the strings of a’s and b’s of
length = 3 exactly
(a+b)³or (a+b)(a+b)(a+b)
The language of all word that have at least two a’s
(a+b)*a(a+b)*a(a+b)*
The language of all strings with exactly two a’s
b*ab*ab*
The language which denotes all the words with at least two a’s
(a+b)*a(a+b)*a(a+b)* = b*ab*a(a+b)*
The language of all the words with exactly two b’s or exactly
two a’s
b*ab*ab* + a*ba*ba*
Theory of Automata
*The only words that do not contain both an a and a b in them, somewhere are the
words of a’s and b’s or Λ
(a+b)*a(a+b)*b(a+b)* + bb*aa*+a*+b*
The language of all words that contain both an a and a b is defined by the expression
(a + b)*a(a + b)*b(a + b)* + bb*aa*
The set of all the strings of a’s and b’s that at some point contain a double letter
(a+b)*(aa+bb)(a+b)*
The language of string of even number of a’s , followed by odd number of b’s or even
number of b’s, followed by odd number of a’s
(aa) *b(bb) * + (bb) *a(aa) *
The language of all words without a double a
b*(abb*)a
*All words that start and end with a different double letter
aa(a+b)*bb(a+b)* + bb(a+b)*aa(a+b)*
aa(a+b)*bb + bb(a+b)*aa
Theory of Automata
The set of all strings of a’s and b’s that have at least two letters, that begin and
end with a’s and that have only b’s in between
ab*a = {aa,aba,abba,abbba,abbbba,...}
All the words that begin with either an a or a c and then are followed by some
number of b’s
(a+c)b* = {a,c,ab,cb,abb,cbb,abbb,cbbb,abbbb,cbbbb,…}
The only words that do not contain both an a and a b in them
a*+b*
All words of the form some positive number of a’s followed by exactly one b
aa*b
All words of strings that contain at most one b and the rest a's
a*(b+Λ)a*
Theory of Automata
The language, consists of strings that are either all a’s or b followed by a
nonnegative number of a’s
a*+ba* or a* (Λ+ b)
The language that denote all words with at least two a’s
b*ab*a(a+b)*
The language of all words in which either the a come before b or the b come before
the a
(a+b)*a(a+b)*b(a+b)* + (a+b)*b(a+b)*a(a+b)*
The language of all strings of a's and b's that have even length
(aa+ab+bb+ba)*or( (a+b)(a+b) )*
*The set of all strings over {a, b, c} that do not contain the substring ac
(c*(a+(bc*))*)
*Need to understand carefully
Theory of Automata
Language of all words that have at least one a and at least one b
?
MUST BE=>
Postfix
ba(a+b)* + ab(a+b)*
All strings with postfix ba or postfix ab
(a+b)*ba + (a+b)*ba
All strings with prefix ab and postfix ba
ab(a+b)*ba
All strings with prefix ba and postfix ab
ba(a+b)*ab
Theory of Automata
(b*ab)*b* + (b*ab)*b*a
All strings with an even number of a’s
(b*ab*ab*)*
Language of all even length strings of alternating a’s and b’s
(ab)* + (ba)*
The set of strings over {a,b} that end in 3 consecutive b's
(a + b)* bbb
The set of strings that have at least one b
a*b(a+b)*
Theory of Automata
Over View:
20
Over View:
21
Over View:
22
23
{Λ} Λ
24
{0} 0
{1}* {10} 1* 10
Note: ( a* b* )* = (a + b )*
Some important expressions equal to :
1* (1 + Λ) = 1*
1* 1* = 1*
0* + 1* = 1* + 0*
(0* 1*)* = (0 + 1)*
(0 + 1)* 01(0 + 1)* + 1* 0* = (0 + 1)*
Theory of Automata
Regular Expression cont…
Write RE for the following languages for Σ = {0,1}
26
?
Theory of Automata
(0+10)*11(0+1)*
Theory of Automata
32
Example No. # 1
33
Example No. # 2
34
Let L be the language of all string of 0's and 1s that have odd length. We
can use the previous example: odd length means in particular length at
least one, and so we may view L as the language of all strings consisting
of single symbol followed by an even-length string. Since we have a
regular expression for even-length strings, and we can easily find one for
strings of length 1, a regular expression for L is
(0 + 1) (00 + 01 + 10 + 11)*
one may ask why we couldn't have described the language in this
example as the set of string consisting of an even-length string followed
by a single symbol, which would have led to
(00 + 01 + 10 + 11)* (0 + 1)
Theory of Automata
Example No. # 3
35
Example No. # 4
36
L = { w is a binary string which does not contain two consecutive 0s or two consecutive 1s
anywhere)
e.g., w = 01010101 is in L, while w = 10010 is not in L
Goal: Build a regular expression for L
Four cases for w:
Case A: w starts with 0 and |w| is even
Case B: w starts with 1 and |w| is even
Case C: w starts with 0 and |w| is odd
Case D: w starts with 1 and |w| is odd
Regular expression for the four cases:
Case A: Case B:
==============================================
(01)* (10)*
Since L is the union of all 4 cases:
R.E for L = (01)* + (10)* + 0(10)* + 1(01)*
If we introduce Λ then the regular expression can be simplified to: Case C: Case D:
R.E for L = (Λ +1)(01)*(Λ +0) 0(10)* 1(01)*
+
(Λ +0)(10)*(Λ +1)
Theory of Automata
Example No. # 5
37
L = {x belongs to {0, 1} where x ends with 1 and does not contain the
sub-string 00}
This mean that every string in L corresponds to the regular expression R
= (1 + 01)*
This extra constraint simply means that Λ can't be included, and that L
corresponds to the regular expression.
(1 + 01)+ = (1 + 01)* (1 + 01)
Theory of Automata
38
Assignment No. # 1
Very Important for Regular Expressions
Theory of Automata
Important R.E.
41
Example:(a + bc) *
= {, a , bc , aa , abc , bca , ….}
44
Advice
46