Regular Expression: Anab Batool Kazmi
Regular Expression: Anab Batool Kazmi
Regular Expression: Anab Batool Kazmi
Theory of Automata 4
Regular Expression Operations
• Union (+, U, | , v)
• OR
• (a+b) means either a or b
• Concatenation (.)
• AND
• (ab) means both a and b
• Kleene Star (*)
• Kleene Plus (+)
Rules to Construct R.E
A Regular expression over a given alphabet Σ is constructed using the
following rules:
1. ^ (the empty string) is a regular expression.
• L(^) = {^}
2. ϕ(the empty language) is a regular expression.
• L(ϕ) ={ } OR {^}
3. Every symbol x in Σ is a regular expression.
4. If R1 and R2 are regular expressions:
• R1 + R2 (the union of two regular expressions) is a regular expression.
• L(R1+R2)=L(R1) U L(R2)
• R1R2 (the concatenation of two regular expressions) is a regular expression.
• L(R1R2)=L(R1).L(R2)
• R1* (the star of a regular expression) is a regular expression.
Precedence of Operations
• kleene > concatenation > union
For example
• The expression a + b*c is grouped to (a + ((b)*c)).
• The expression 01*+1 is grouped to (0(1)*)+1 .
Closure Properties
Kleene Star (*) /Kleene Closure Kleene Plus (+) / Positive Closure
• The Kleene Star indicates zero or • The Kleene Plus indicates one or
more occurrence of preceding more occurrence of preceding
element. element.
• Represented by “*”. • Represented by “+”.
Example Example
If Σ={a} , define the language L1 of all If Σ={a} , define the language L2 of
strings including null. all strings excluding null.
L1={^, a, aa, aaa,…} L2={a, aa, aaa,…}
R.E(L1)= a* R.E(L2)= a+
•* •+
• * = {0,1,2,3,….} • + = {1,2,3,….}
• a^0= ^ • a^+ ={ a, aa, aaa,aaaa,… }
• a^2 =aa
• a^3 =aaa
• a* = { ^, a, aa, aaa,aaaa,… }
Example
Find the regular expression for set of all strings whose length is exactly
equal to 2, defined over Σ = {a, b}. .
Descriptive form:
L={aa,ab,ba,bb}
Regular Expression:
R.E(Σ)=aa+ab+ba+bb
=a(a+b)+b(a+b)
R.E=(a+b)(a+b)
Example
Find the regular expression for set of all strings whose length is exactly
equal to 3, defined over Σ = {a, b}. .
Descriptive form:
L={aaa,aab,aba,abb,baa,bab,bba,bbb}
Regular Expression:
R.E(Σ)= aaa+aab+aba+abb+baa+bab+bba+bbb
=aa(a+b)+ab(a+b)+ba(a+b)+bb(a+b)
=(a+b)(aa+ab+ba+bb)
R.E=(a+b)(a+b)(a+b)
Example
Find the regular expression for set of all strings whose length is exactly equal to 4,
defined over Σ = {a, b}?
R.E= (a+b).(a+b).(a+b).(a+b)
Find the regular expression for set of all strings whose length is exactly equal to 2,
defined over Σ = {a, b, c}?
R.E= (a+b+c).(a+b+c)
Find the regular expression for set of all strings whose length is exactly equal to 2,
defined over Σ = {a}?
R.E= aa
Example
Language L, consisting of all possible strings, defined over Σ = {a, b}.
Descriptive form:
L={^,a,b,aa,ab,ba,bb,…}
Regular Expression:
R.E(Σ)=(a + b)*.
Language L, consisting of all possible strings, defined over Σ = {a}.
R.E =(a )*
Language L, consisting of all possible strings, defined over Σ = {a,b,c}.
R.E =(a+b+c )*
• Language L, consisting of all possible strings, defined over Σ = {a, b}.
• LENGTH = { 0,1,2,3,……}
• LENGTH R.E = ^ = (a + b)^0
• Length 1 R.E= a+b = (a + b)^1
• Length 2 R.E= (a + b). (a + b) = (a + b)^2
• Length 3 R.E= (a + b). (a + b) . (a + b)
• R.E = (a + b)*
• Language L, consisting of all possible strings EXCLUDING NULL,
defined over Σ = {a, b, c}.
• R.E = (a + b + c)^+
Language L, consisting of all possible strings excluding null, defined over
Σ = {a,b,c}.
Or
Ex
String of length 3
=
=. .
Example
Find the regular expression for set of all strings whose length is atleast
equal to 2, defined over Σ = {a, b}.
Descriptive form:
L={aa,ab,ba,bb ,aaa,aab,aba,abb,baa,bab,bba,bbb,…}
Regular Expression:
R.E(Σ)=(a+b)(a+b) (a+b)*
(a+b)(a+b)(a+b)^0 =(a+b)(a+b).^
(a+b)(a+b)(a+b)^1= (a+b)(a+b)(a+b)
(a+b)(a+b)(a+b)^2=(a+b)(a+b)(a+b)(a+b)
Example
Find the regular expression for set of all strings whose length is atmost
equal to 2, defined over Σ = {a, b, c}.
Descriptive form:
L={^,a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc}
Regular Expression:
R.E(Σ)=(a+b+^+c)(a+b+^+c)
Example
Find the regular expression for set of all strings of even length, defined over Σ = {a, b}.
Descriptive form:
L={^,aa,ab,ba,bb,aaaa,aabb,…}
Regular Expression:
R.E(Σ)=((a+b)(a+b))*
Set of all strings of even length but null is not included(2,4,6,…)
R.E=
OR
R.E= (a+b)(a+b)((a+b)(a+b))*
Odd length = 1,3,5,7,….
Find the regular expression for set of all strings of even length, defined over Σ = {a,
b}.
Descriptive form:
L={^,aa,ab,ba,bb,aaaa,aabb,…}
Regular Expression:
R.E(Σ)=((a+b)(a+b))*
= LENGTH 2
= LENGTH 4
= LENGTH 6
Example
Find the regular expression for set of all strings of odd length, defined
over Σ = {a, b}.
Descriptive form:
L={a,b,aaa,aab,aba,…}
Regular Expression:
R.E(Σ)=(a+b)((a+b)(a+b))*
Example
Language L, of strings having exactly one a, defined over Σ = {a, b}.
Descriptive form:
L={a,ab,ba,abb,bab,…}
Regular Expression:
R.E(Σ)=b*a b*.
Example
Language L, of strings having atleast one a, defined over Σ = {a, b}.
Descriptive form:
L={a,aa,ab,ba,aaa,aab,aba,abb,baa,bab…}
Regular Expression:
R.E(Σ)= (a + b)* a (a + b)*
Example
Language L, of strings having all the words that begin with a, followed
by anything (i.e., as many choices as we want of either a or b) , defined
over Σ = {a, b}.
Descriptive form:
L={a,aa,ab,abb,aba,…}
Regular Expression:
R.E(Σ)= a(a + b)*.
Example
Language L of strings of a’s and b’s ending with the string abb.
Descriptive form:
L= {abb, aabb, babb, aaabb, ababb, …………..}
Regular Expression:
R.E(Σ)=(a+b)*abb.
Example
Language L of strings consisting of even number of 1’s including empty
string.
Descriptive form:
L= {^, 11, 1111, 111111, ……….}
Regular Expression:
R.E(Σ)=(11)*
Set of strings consisting of even number of a’s followed by odd
number of b’s .
Descriptive form:
L = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb, …………..}
Regular Expression:
(aa)*(bb)*b
Equivalent Regular Expressions
• Two regular expressions are equal if they generate the same
language.
Example:
Consider the following regular expressions
• r1 = (a + b)* (aa + bb)
• r2 = (a + b)*aa + ( a + b)*bb
both regular expressions define the language of strings ending in aa or
bb.
Regular Expressions and Programming
Languages
• An identifier in the C programming language is a string of length 1 or
more that contains only letters, digits, and underscores (“ _”) and does
not begin with a digit.
• int _num123NUM _123; // where num is an identifier