SlideShare a Scribd company logo
1 of 45
Regular Expressions
(Examples)
[Week#03] (a) - Regular
Expressions (Examples)
Course: Theory of Automata
Topic: Regular Expressions (Examples)
Instructor: Mr. Muhammad Arif
Regular Expression
Write RE for the following languages for Σ = {a,b}
[Week#03] (a) - Regular
Expressions (Examples)
• The language of all words
(a+b)*
• All words ending with b
(a+b)*b
• All words that start with a
a(a+b)*
• The language of all strings, not beginning with b
a(a+b)*+ Λ
• All words that start with a double letter
(aa+bb)(a+b)*
• All words that contain at least one double letter
(a+b)*(aa+bb)(a+b)*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• All words that contain at least two a’s or two b’s
b*ab*ab* + a*ba*ba*
• All words that start and end with a double letter
(aa+bb)(a+b)*(aa+bb)
• All words of length >=3
(a+b)(a+b)(a+b)(a+b)* or (a+b)(a+b)(a+b)+
• All words that contain exactly one a or exactly one b
• b*ab* + a*ba* t
• All words that don’t end at ba
(a+b)*(aa+ab+bb)
• All strings of a’s and b’s in which either the strings are all b’s or else there
is an a followed by some b’s
b*+ab*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• Language of all words that have at least two a’s
(a+b)* a (a+b)* a (a+b)*
• Language of all words that have at least one a and at least one b
(a+b)* a (a+b)* b (a+b)* + (a+b)* b (a+b)* a (a+b)*
• Language of all words that have at least one a or at least one b
(a+b)*a(a+b)* + (a+b)*b(a+b)*
• The languages L, of even length, defined over Σ = {a, b}
((a+b)(a+b))*
• The languages L, of odd length, defined over Σ = {a, b}
((a+b)(a+b))*(a+b)
• The strings of length 2, starting with a,
aa+ab
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• 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))*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• ODD-ODD (Σ = {a, b})
• // Think and Solve Your Own
ODD-ODD (Σ = {a, b}) i.e. =
{a,b,ab,ba,aaa,bbb,abbb,aaabbbbb, …}
RE sets:
R1 = (a+b)((a+b)(a+b))*
R2 = (ab+ba)((ab+ba)(ab+ba))*
=>So,
R.E. For ODD-ODD =
(a+b)( (a+b)(a+b) )* + (ab+ba)( (ab+ba)(ab+ba)* )
or
(a + b)(aa + bb + ab + ba)*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
•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
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• 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*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• *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
*need to understand carefully
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• String of all words having exactly two a’s
b*ab*ab*
• Language of all strings of words, starting with a and ending
with b or starting with b and ending with a
a(a+b)*b + b(a+b)*a
• The String of all words whose length(x) < 3, starting with ba
ba
• The String of words, starting with double b and ending with
either a or b
bb(a+b)
• All the strings ending at aa or bb
(a+b)*(aa+bb) or (a+b)*aa + (a+b)*bb
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• Any string that contains at least two consecutive a’s
(a+b)*aa(a+b)* // Need to Think
• All words that don’t ends at ba
(a+b)*(aa+ab+bb)
• All words, starting with double letter
(aa+bb)(a+b)*
• All words that contains at least one double letter
(a+b)*(aa+bb)(a+b)*
• *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
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• All words of length > 3
(a+b)(a+b)(a+b)(a+b)(a+b)* or (a+b)(a+b)(a+b)(a+b)+
• All word of length >= 3
(a+b)(a+b)(a+b)(a+b)* or (a+b)(a+b)(a+b)+
• All words that start and end with a double letter
aa(a+b)*bb + bb(a+b)*aa or (aa+bb)(a+b)*(aa+bb)
• All words that contain exactly one a or one b
b*ab*+a*ba*
• All words that contain exactly two a’s or exactly two b’s
(a+b)*aa(a+b)* + (a+b)*bb(a+b)*
• The language of string in which any no. of a’s may occur before, between,
and after the b’s
a*ba*ba*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• All words that contains at least two a’s
b*ab*ab*
• All words with at least two a’s
b*ab*a(a+b)*
• All words with exactly two a’s
b*ab*ab*
• Language that defines all even-length strings of alternating a’s and b’s
(ab)*+(ba)*
• Language that defines all odd-length strings of alternating a’s and b’s
?
(a + b)(aa + bb + ab + ba)*
• The set of strings with an even number of a's followed by an odd number of b's
(aa)*(bb)*b
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• The set of strings over {a, b} that contains the substrings aa or bb
(a+b)*aa(a+b)* + (a+b)*bb(a+b)*
• The set of string over { a, b } that do not contain the substrings aa and bb
(ab)* - (ab)*aa(ab)* + (ab)*bb(ab)*
• The strings that start with aa, end with bb, and have alternating substrings
ba in between
a(ab)+ b or aa(ba)*bb
• The strings that contain at most one b and the rest a's
a*(b+Λ)a*
• The even length strings of a's and b's
(aa+bb+ab+ba)*
• The odd length strings of a's and b's
(a+b)(aa+bb+ab+ba)*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• 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*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• 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
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
•Language of all words that have at least one a and at least one b
(a+b)* a(a+b)* b(a+b)*
What about the word ba
?
MUST BE =>
(a+b)* a(a+b)* b(a+b)* + (a+b)* b(a+b)* a(a+b)*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• All strings with prefix ab
ab(a+b)*
• All strings with postfix ab
(a+b)ab*
• All strings with prefix ba or prefix ab
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
Prefix
Postfix
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• All strings that have two consecutive a’s
(a+b)*aa(a+b)*
• All strings except those with two consecutive a’s
(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)*
Over View:
[Week#03] (a) - Regular
Expressions (Examples)
• The language L of strings of odd length, defined over Σ={a}, can be
written as L={a, aaa, aaaaa,…..}
• The language L of strings that does not start with a, defined over Σ={a,b,c},
can be written as L={b, c, ba, bb, bc, ca, cb, cc, …}
• The language L of strings of length 2, defined over Σ={0,1,2}, can be
written as L={00, 01, 02,10, 11,12,20,21,22}
• The language L of strings ending in 0, defined over Σ ={0,1}, can be
written as L={0,00,10,000,010,100,110,…}
• The language EQUAL, of strings with number of a’s equal to number of
b’s, defined over Σ={a,b} = {Λ ,ab,aabb,abab,baba,abba,…}
• The language EVEN-EVEN, of strings with even number of a’s and even
number of b’s, defined over Σ={a,b}, can be written as
{Λ, aa, bb, aaaa,aabb,abab, abba, baab, baba, bbaa, bbbb,…}
Over View:
[Week#03] (a) - Regular
Expressions (Examples)
• The language INTEGER, of strings defined over Σ={-,0,1,2,3,4,5,6,7,8,9},
can be written as INTEGER = {…,-2,-1,0,1,2,…}
• The language EVEN, of stings defined over Σ={-,0,1,2,3,4,5,6,7,8,9}, can
be written as EVEN = { …,-4,-2,0,2,4,…}
• The language {anbn}, of strings defined over Σ={a,b}, as {anbn :
n=1,2,3,…}, can be written as {ab, aabb, aaabbb,aaaabbbb,…}
• The language {anbn an}, of strings defined over Σ={a,b}, as {anbn
an : n=1,2,3,…}, can be written as {aba, aabbaa,
aaabbbaaa,aaaabbbbaaaa,…}
• The language factorial, of strings defined over Σ={1,2,3,4,5,6,7,8,9} i.e.
{1,2,6,24,120,…}
• The language FACTORIAL, of strings defined over Σ={a}, as {an! :
n=1,2,3,…}, can be written as {a,aa,aaaaaa,…}
It is to be noted that the language FACTORIAL can be defined over any
single letter alphabet.
Over View:
[Week#03] (a) - Regular
Expressions (Examples)
• The language DOUBLEFACTORIAL, of strings defined over Σ={a, b},
as {an! bn! : n=1,2,3,…}, can be written as {ab, aabb, aaaaaabbbbbb,…}
• The language SQUARE, of strings defined over Σ={a}, as {an2
:
n=1,2,3,…}, can be written as {a, aaaa, aaaaaaaaa,…}
• The language DOUBLESQUARE, of strings defined over Σ={a,b},
as{an2
bn2
: n=1,2,3,…}, can be written as {ab, aaaabbbb,
aaaaaaaaabbbbbbbbb,…}
• The language PRIME, of strings defined over Σ={a}, as
{ap : p is prime}, can be written as {aa,aaa,aaaaa,aaaaaaa,aaaaaaaaaaa…}
• // Total Examples Defined over {a,b} = 100
Languages and Regular Expressions
[Week#03] (a) - Regular
Expressions (Examples)
Regular Expression cont…
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
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)*
Regular Expression cont…
Write RE for the following languages for Σ = {0,1}
[Week#03] (a) - Regular
Expressions (Examples)
 The set of strings over {0,1} that end in 3 consecutive 1's.
(0 + 1)* 111
 The set of strings over {0,1} that have at least one 1
0* 1 (0 + 1) *
 The language that consists of all strings where the length of any run
of consecutive 0's is a multiple of 3
(1 + 000)*
 The language of all strings that end in 1101
(1 + 0)*1101
 Language that defines all even-length strings of alternating 0s and1s,
where Σ = {0,1}
(01)* + (10)*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• The language of those binary strings, that is, strings on the
alphabet {0, 1}, that contain the substring 1011
(0+1)*1011(0+1)*
• The language of all binary strings where every run of
consecutive 1's has even length
(0 + 11)*
• The language of all binary strings that do not contain the
substring 1011
((0*11*00)*0*11*0(10)*0)*(0*+11*+11*0(10)*+11*0(10)*1)
• The set of all strings over {0,1} that do not have the substring
111
(0*+(((0*(1+(11)))((00*)(1+(11)))*)0*))
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• Language of all even length strings of alternating 0s and 1s
(01)* + (10)*
• The language of all strings of 0's and 1's that have odd length
(0 + 1)(00 + 01 + 10 + 11)*
• Set of all strings with any number of “0”s followed by any number of 1s
0*1*
• All strings that have two consecutive 0s
(0+1)*00(0+1)*
• All strings except those with two consecutive 0s
(1*01)*1* + (1*01)*1*0
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• All strings with an even number of 0s
(1*01*01*)*
• All the strings of length 2
(00 + 01 + 10 + 11)*
• Language of all strings that ends in 1 and doesn’t contain the substring 00
(1+01)*
• All strings except those with two consecutive 0’s
(1*01)*1* + (1*01)*1*0
• All strings with an even number of 0’s
(1*01*01*)*
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
• Language of all even length strings of alternating 0’s and 1’s
(01)* + (10)*
• The set of strings over {0,1} that end in 3 consecutive b's
(0 + 1)* 111
• The set of strings that have at least one 1
0*1(0+1)*
• All strings without substring 001
?
Think & Do Your Self…
[Week#03] (a) - Regular
Expressions (Examples)
• Question: Write a regular expression for the
set of strings that contains an even number of
1’s over ={0,1}. Treat zero 1’s as an even
number.
• Answer:
(0+10)*11(0+1)*
Important Examples & Applications
[Week#03] (a) - Regular
Expressions (Examples)
Regular Expressions cont…
Example No. # 1
[Week#03] (a) - Regular
Expressions (Examples)
• Let L be the language of all strings of 0s and 1s that have even
length, (Since 0 is even, L contains Λ). Is L regular, and if so,
what is a regular expression corresponding to it?
• We can answer this by realizing that if a string has even length, it
can be thought of as consisting of a number, possibly zero, of
string of length 2 concatenated.
• And, conversely, any such concatenation has even length.
• Since we can easily enumerate the strings of length 2, we may
write the answer:
(00 + 01 + 10 + 11)*
Example No. # 2
[Week#03] (a) - Regular
Expressions (Examples)
• 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)
Example No. # 3
[Week#03] (a) - Regular
Expressions (Examples)
• Let L be the language of all strings of 0s and 1s containing at least one 1.
• Here are three regular expressions corresponding to L:
- 0* 1 (0 + 1)*
- (0 + 1)* 1 (0 + 1)*
- (0 + 1)* 10*
• The first expresses the fact that a string in L can be decomposed as follows: an
arbitrary number of 0's (possibly none), the first 1, and then any arbitrary string.
• The second, which is some sense is the most general, or the closest to our definition
of L, expresses the fact that a string in L has a 1, both preceded and followed by an
arbitrary string.
• The third is similar to the first, but emphasized the last 1 in string in L.
Example No. # 4
[Week#03] (a) - Regular
Expressions (Examples)
• 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:
==============================================
• 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:
– R.E for L = (Λ +1)(01)*(Λ +0)
+
(Λ +0)(10)*(Λ +1)
Case A:
(01)*
Case B:
(10)*
Case C:
0(10)*
Case D:
1(01)*
Example No. # 5
[Week#03] (a) - Regular
Expressions (Examples)
• 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)
Assignment No. # 1
[Week#03] (a) - Regular
Expressions (Examples)
Very Important for Regular Expressions
[Week#03] (a) - Regular
Expressions (Examples)
Assignment No. # 1 (Solution)
[Week#03] (a) - Regular
Expressions (Examples)
• The language of all string not containing the substring 000
(1+0)*10*+ (1+0)*0*1+ (1+0)*
• The language of all string that do not contain substring 110
0*1(1+0)*+10*(1+0)*+ (1+0)*
• The language of all strings containing both 101 & 010 as substring
1*(1+0)*1*0*
• The language of all strings in which both the number of 0’s and the number of 1’s are Even
(11+00+ (10+01)(11+00)*(10+01))*
• The language of all strings in which both the number of 0’s and the number of 1’s are Odd
(11+00+ (10+01)(11+00)*(10+01))*(1+0)
• All words that contain Exactly two1’s or three 1’s,not more
11(1+0)*0*+0*1(1+0)*1
• All string that have exactly one double letter in them
(1* 01*01*)+ (0*10*10*)
• All the word in which 1 is tripled or 0 is tripled , but not both
(1* 01*01*01*) + (0*10*10*10*)
• All the String in which the total no of 1’s is divisible by 3,no matter how they are arrange such as 1101100101
((0*10*10*10*))*
• All the words in which any 0’s that occurs are found in clumps of odd number at a time , such as 1101100010
1*(00+01)*0 (1)*
Important R.E.
[Week#03] (a) - Regular
Expressions (Examples)
• Write a R.E for set of strings over {a,b}
that do not contain the substring aa
• A string in this set may contain a prefix of
any number of b’s.
• All a’s must be followed by at least one b or
terminate the string.
• The R.E b*(ab+) + b*(ab+)*a generates the
desired set.
• =>
• b*(ab+)*(Λ+ a) = b*(abb*)*(Λ + a) = (b +
ab)*(Λ + a)
Regular Expression cont…
[Week#03] (a) - Regular
Expressions (Examples)
Note: Regular expressions describe regular languages
Example: (a + bc) *
= {, a , bc , aa , abc , bca , ….}
// Total Examples Defined over {0,1} = 40
Regular Expression cont…
Example: (Solved the Un-Solved)
[Week#03] (a) - Regular
Expressions (Examples)
1. 01* = {0, 01, 011, 0111, …..}
2. (01*)(01) = {001, 0101, 01101, 011101, …..}
3. (0+1)*
4. (0+1)*01(0+1)*
5. ((0+1)(0+1)+(0+1)(0+1)(0+1))*
6. ((0+1)(0+1))*+((0+1)(0+1)(0+1))*
7. (1+01+001)*(Λ+0+00)
How to make and check Regular
Expression Easily?
[Week#03] (a) - Regular
Expressions (Examples)
Regular Expression cont…
Regular Expression cont…
Example
[Week#03] (a) - Regular
Expressions (Examples)
Regular expression:
  *
a
b
a 

 
 
*
a
b
a
L 
  
   
*
a
L
b
a
L 

   
*
a
L
b
a
L 

   
   
 *
a
L
b
L
a
L 

   
   
 *
a
b
a 

  
,...
,
,
,
, aaa
aa
a
b
a 

 
,...
,
,
,...,
,
, baa
ba
b
aaa
aa
a

Exercise:
[Week#03] (a) - Regular
Expressions (Examples)
• Practice the Examples of Regular Expressions
where Σ={ i , j , k }

More Related Content

What's hot

Theory of Automata
Theory of AutomataTheory of Automata
Theory of AutomataFarooq Mian
 
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...Farwa Ansari
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOCAbhayDhupar
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Generalized transition graphs
Generalized transition graphsGeneralized transition graphs
Generalized transition graphsArham Khan G
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFAMaulik Togadiya
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDAAshish Duggal
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02hamzamughal39
 
Code Optimization
Code OptimizationCode Optimization
Code Optimizationguest9f8315
 

What's hot (20)

Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lesson 08
Lesson 08Lesson 08
Lesson 08
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Language
LanguageLanguage
Language
 
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
 
FInite Automata
FInite AutomataFInite Automata
FInite Automata
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Generalized transition graphs
Generalized transition graphsGeneralized transition graphs
Generalized transition graphs
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Lesson 05
Lesson 05Lesson 05
Lesson 05
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 

Similar to REGULAR EXPRESSIONS

regular expressions (Regex)
regular expressions (Regex)regular expressions (Regex)
regular expressions (Regex)Rebaz Najeeb
 
Handout Regular expression with examples and lecture
Handout Regular expression with examples  and lecture Handout Regular expression with examples  and lecture
Handout Regular expression with examples and lecture mariajan8
 
12 -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]
12  -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]12  -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]
12 -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]Rabi Iftikhar
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxRaviAr5
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Toc chapter 1 srg
Toc chapter 1 srgToc chapter 1 srg
Toc chapter 1 srgShayak Giri
 
theory of computation notes. introduction and mcq points
theory of computation notes. introduction and mcq pointstheory of computation notes. introduction and mcq points
theory of computation notes. introduction and mcq pointsbushraphd2022
 

Similar to REGULAR EXPRESSIONS (18)

Automata theory -RE to NFA-ε
Automata theory -RE to  NFA-εAutomata theory -RE to  NFA-ε
Automata theory -RE to NFA-ε
 
13000120020_A.pptx
13000120020_A.pptx13000120020_A.pptx
13000120020_A.pptx
 
Lesson 09.ppt
Lesson 09.pptLesson 09.ppt
Lesson 09.ppt
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
regular expressions (Regex)
regular expressions (Regex)regular expressions (Regex)
regular expressions (Regex)
 
Handout Regular expression with examples and lecture
Handout Regular expression with examples  and lecture Handout Regular expression with examples  and lecture
Handout Regular expression with examples and lecture
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 
12 -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]
12  -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]12  -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]
12 -csc312_automat_theory_assign-1_(27-09-12)_(1)[1]
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Math symbols table
Math symbols   tableMath symbols   table
Math symbols table
 
Toc chapter 1 srg
Toc chapter 1 srgToc chapter 1 srg
Toc chapter 1 srg
 
theory of computation notes. introduction and mcq points
theory of computation notes. introduction and mcq pointstheory of computation notes. introduction and mcq points
theory of computation notes. introduction and mcq points
 
Vectors
VectorsVectors
Vectors
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

REGULAR EXPRESSIONS

  • 1. Regular Expressions (Examples) [Week#03] (a) - Regular Expressions (Examples) Course: Theory of Automata Topic: Regular Expressions (Examples) Instructor: Mr. Muhammad Arif
  • 2. Regular Expression Write RE for the following languages for Σ = {a,b} [Week#03] (a) - Regular Expressions (Examples) • The language of all words (a+b)* • All words ending with b (a+b)*b • All words that start with a a(a+b)* • The language of all strings, not beginning with b a(a+b)*+ Λ • All words that start with a double letter (aa+bb)(a+b)* • All words that contain at least one double letter (a+b)*(aa+bb)(a+b)*
  • 3. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • All words that contain at least two a’s or two b’s b*ab*ab* + a*ba*ba* • All words that start and end with a double letter (aa+bb)(a+b)*(aa+bb) • All words of length >=3 (a+b)(a+b)(a+b)(a+b)* or (a+b)(a+b)(a+b)+ • All words that contain exactly one a or exactly one b • b*ab* + a*ba* t • All words that don’t end at ba (a+b)*(aa+ab+bb) • All strings of a’s and b’s in which either the strings are all b’s or else there is an a followed by some b’s b*+ab*
  • 4. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • Language of all words that have at least two a’s (a+b)* a (a+b)* a (a+b)* • Language of all words that have at least one a and at least one b (a+b)* a (a+b)* b (a+b)* + (a+b)* b (a+b)* a (a+b)* • Language of all words that have at least one a or at least one b (a+b)*a(a+b)* + (a+b)*b(a+b)* • The languages L, of even length, defined over Σ = {a, b} ((a+b)(a+b))* • The languages L, of odd length, defined over Σ = {a, b} ((a+b)(a+b))*(a+b) • The strings of length 2, starting with a, aa+ab
  • 5. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • 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))*
  • 6. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • ODD-ODD (Σ = {a, b}) • // Think and Solve Your Own ODD-ODD (Σ = {a, b}) i.e. = {a,b,ab,ba,aaa,bbb,abbb,aaabbbbb, …} RE sets: R1 = (a+b)((a+b)(a+b))* R2 = (ab+ba)((ab+ba)(ab+ba))* =>So, R.E. For ODD-ODD = (a+b)( (a+b)(a+b) )* + (ab+ba)( (ab+ba)(ab+ba)* ) or (a + b)(aa + bb + ab + ba)*
  • 7. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) •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
  • 8. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • 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*
  • 9. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • *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 *need to understand carefully
  • 10. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • String of all words having exactly two a’s b*ab*ab* • Language of all strings of words, starting with a and ending with b or starting with b and ending with a a(a+b)*b + b(a+b)*a • The String of all words whose length(x) < 3, starting with ba ba • The String of words, starting with double b and ending with either a or b bb(a+b) • All the strings ending at aa or bb (a+b)*(aa+bb) or (a+b)*aa + (a+b)*bb
  • 11. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • Any string that contains at least two consecutive a’s (a+b)*aa(a+b)* // Need to Think • All words that don’t ends at ba (a+b)*(aa+ab+bb) • All words, starting with double letter (aa+bb)(a+b)* • All words that contains at least one double letter (a+b)*(aa+bb)(a+b)* • *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
  • 12. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • All words of length > 3 (a+b)(a+b)(a+b)(a+b)(a+b)* or (a+b)(a+b)(a+b)(a+b)+ • All word of length >= 3 (a+b)(a+b)(a+b)(a+b)* or (a+b)(a+b)(a+b)+ • All words that start and end with a double letter aa(a+b)*bb + bb(a+b)*aa or (aa+bb)(a+b)*(aa+bb) • All words that contain exactly one a or one b b*ab*+a*ba* • All words that contain exactly two a’s or exactly two b’s (a+b)*aa(a+b)* + (a+b)*bb(a+b)* • The language of string in which any no. of a’s may occur before, between, and after the b’s a*ba*ba*
  • 13. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • All words that contains at least two a’s b*ab*ab* • All words with at least two a’s b*ab*a(a+b)* • All words with exactly two a’s b*ab*ab* • Language that defines all even-length strings of alternating a’s and b’s (ab)*+(ba)* • Language that defines all odd-length strings of alternating a’s and b’s ? (a + b)(aa + bb + ab + ba)* • The set of strings with an even number of a's followed by an odd number of b's (aa)*(bb)*b
  • 14. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • The set of strings over {a, b} that contains the substrings aa or bb (a+b)*aa(a+b)* + (a+b)*bb(a+b)* • The set of string over { a, b } that do not contain the substrings aa and bb (ab)* - (ab)*aa(ab)* + (ab)*bb(ab)* • The strings that start with aa, end with bb, and have alternating substrings ba in between a(ab)+ b or aa(ba)*bb • The strings that contain at most one b and the rest a's a*(b+Λ)a* • The even length strings of a's and b's (aa+bb+ab+ba)* • The odd length strings of a's and b's (a+b)(aa+bb+ab+ba)*
  • 15. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • 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*
  • 16. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • 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
  • 17. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) •Language of all words that have at least one a and at least one b (a+b)* a(a+b)* b(a+b)* What about the word ba ? MUST BE => (a+b)* a(a+b)* b(a+b)* + (a+b)* b(a+b)* a(a+b)*
  • 18. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • All strings with prefix ab ab(a+b)* • All strings with postfix ab (a+b)ab* • All strings with prefix ba or prefix ab 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 Prefix Postfix
  • 19. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • All strings that have two consecutive a’s (a+b)*aa(a+b)* • All strings except those with two consecutive a’s (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)*
  • 20. Over View: [Week#03] (a) - Regular Expressions (Examples) • The language L of strings of odd length, defined over Σ={a}, can be written as L={a, aaa, aaaaa,…..} • The language L of strings that does not start with a, defined over Σ={a,b,c}, can be written as L={b, c, ba, bb, bc, ca, cb, cc, …} • The language L of strings of length 2, defined over Σ={0,1,2}, can be written as L={00, 01, 02,10, 11,12,20,21,22} • The language L of strings ending in 0, defined over Σ ={0,1}, can be written as L={0,00,10,000,010,100,110,…} • The language EQUAL, of strings with number of a’s equal to number of b’s, defined over Σ={a,b} = {Λ ,ab,aabb,abab,baba,abba,…} • The language EVEN-EVEN, of strings with even number of a’s and even number of b’s, defined over Σ={a,b}, can be written as {Λ, aa, bb, aaaa,aabb,abab, abba, baab, baba, bbaa, bbbb,…}
  • 21. Over View: [Week#03] (a) - Regular Expressions (Examples) • The language INTEGER, of strings defined over Σ={-,0,1,2,3,4,5,6,7,8,9}, can be written as INTEGER = {…,-2,-1,0,1,2,…} • The language EVEN, of stings defined over Σ={-,0,1,2,3,4,5,6,7,8,9}, can be written as EVEN = { …,-4,-2,0,2,4,…} • The language {anbn}, of strings defined over Σ={a,b}, as {anbn : n=1,2,3,…}, can be written as {ab, aabb, aaabbb,aaaabbbb,…} • The language {anbn an}, of strings defined over Σ={a,b}, as {anbn an : n=1,2,3,…}, can be written as {aba, aabbaa, aaabbbaaa,aaaabbbbaaaa,…} • The language factorial, of strings defined over Σ={1,2,3,4,5,6,7,8,9} i.e. {1,2,6,24,120,…} • The language FACTORIAL, of strings defined over Σ={a}, as {an! : n=1,2,3,…}, can be written as {a,aa,aaaaaa,…} It is to be noted that the language FACTORIAL can be defined over any single letter alphabet.
  • 22. Over View: [Week#03] (a) - Regular Expressions (Examples) • The language DOUBLEFACTORIAL, of strings defined over Σ={a, b}, as {an! bn! : n=1,2,3,…}, can be written as {ab, aabb, aaaaaabbbbbb,…} • The language SQUARE, of strings defined over Σ={a}, as {an2 : n=1,2,3,…}, can be written as {a, aaaa, aaaaaaaaa,…} • The language DOUBLESQUARE, of strings defined over Σ={a,b}, as{an2 bn2 : n=1,2,3,…}, can be written as {ab, aaaabbbb, aaaaaaaaabbbbbbbbb,…} • The language PRIME, of strings defined over Σ={a}, as {ap : p is prime}, can be written as {aa,aaa,aaaaa,aaaaaaa,aaaaaaaaaaa…} • // Total Examples Defined over {a,b} = 100
  • 23. Languages and Regular Expressions [Week#03] (a) - Regular Expressions (Examples) Regular Expression cont…
  • 24. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) 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)*
  • 25. Regular Expression cont… Write RE for the following languages for Σ = {0,1} [Week#03] (a) - Regular Expressions (Examples)  The set of strings over {0,1} that end in 3 consecutive 1's. (0 + 1)* 111  The set of strings over {0,1} that have at least one 1 0* 1 (0 + 1) *  The language that consists of all strings where the length of any run of consecutive 0's is a multiple of 3 (1 + 000)*  The language of all strings that end in 1101 (1 + 0)*1101  Language that defines all even-length strings of alternating 0s and1s, where Σ = {0,1} (01)* + (10)*
  • 26. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • The language of those binary strings, that is, strings on the alphabet {0, 1}, that contain the substring 1011 (0+1)*1011(0+1)* • The language of all binary strings where every run of consecutive 1's has even length (0 + 11)* • The language of all binary strings that do not contain the substring 1011 ((0*11*00)*0*11*0(10)*0)*(0*+11*+11*0(10)*+11*0(10)*1) • The set of all strings over {0,1} that do not have the substring 111 (0*+(((0*(1+(11)))((00*)(1+(11)))*)0*))
  • 27. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • Language of all even length strings of alternating 0s and 1s (01)* + (10)* • The language of all strings of 0's and 1's that have odd length (0 + 1)(00 + 01 + 10 + 11)* • Set of all strings with any number of “0”s followed by any number of 1s 0*1* • All strings that have two consecutive 0s (0+1)*00(0+1)* • All strings except those with two consecutive 0s (1*01)*1* + (1*01)*1*0
  • 28. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • All strings with an even number of 0s (1*01*01*)* • All the strings of length 2 (00 + 01 + 10 + 11)* • Language of all strings that ends in 1 and doesn’t contain the substring 00 (1+01)* • All strings except those with two consecutive 0’s (1*01)*1* + (1*01)*1*0 • All strings with an even number of 0’s (1*01*01*)*
  • 29. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) • Language of all even length strings of alternating 0’s and 1’s (01)* + (10)* • The set of strings over {0,1} that end in 3 consecutive b's (0 + 1)* 111 • The set of strings that have at least one 1 0*1(0+1)* • All strings without substring 001 ?
  • 30. Think & Do Your Self… [Week#03] (a) - Regular Expressions (Examples) • Question: Write a regular expression for the set of strings that contains an even number of 1’s over ={0,1}. Treat zero 1’s as an even number. • Answer: (0+10)*11(0+1)*
  • 31. Important Examples & Applications [Week#03] (a) - Regular Expressions (Examples) Regular Expressions cont…
  • 32. Example No. # 1 [Week#03] (a) - Regular Expressions (Examples) • Let L be the language of all strings of 0s and 1s that have even length, (Since 0 is even, L contains Λ). Is L regular, and if so, what is a regular expression corresponding to it? • We can answer this by realizing that if a string has even length, it can be thought of as consisting of a number, possibly zero, of string of length 2 concatenated. • And, conversely, any such concatenation has even length. • Since we can easily enumerate the strings of length 2, we may write the answer: (00 + 01 + 10 + 11)*
  • 33. Example No. # 2 [Week#03] (a) - Regular Expressions (Examples) • 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)
  • 34. Example No. # 3 [Week#03] (a) - Regular Expressions (Examples) • Let L be the language of all strings of 0s and 1s containing at least one 1. • Here are three regular expressions corresponding to L: - 0* 1 (0 + 1)* - (0 + 1)* 1 (0 + 1)* - (0 + 1)* 10* • The first expresses the fact that a string in L can be decomposed as follows: an arbitrary number of 0's (possibly none), the first 1, and then any arbitrary string. • The second, which is some sense is the most general, or the closest to our definition of L, expresses the fact that a string in L has a 1, both preceded and followed by an arbitrary string. • The third is similar to the first, but emphasized the last 1 in string in L.
  • 35. Example No. # 4 [Week#03] (a) - Regular Expressions (Examples) • 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: ============================================== • 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: – R.E for L = (Λ +1)(01)*(Λ +0) + (Λ +0)(10)*(Λ +1) Case A: (01)* Case B: (10)* Case C: 0(10)* Case D: 1(01)*
  • 36. Example No. # 5 [Week#03] (a) - Regular Expressions (Examples) • 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)
  • 37. Assignment No. # 1 [Week#03] (a) - Regular Expressions (Examples) Very Important for Regular Expressions
  • 38. [Week#03] (a) - Regular Expressions (Examples)
  • 39. Assignment No. # 1 (Solution) [Week#03] (a) - Regular Expressions (Examples) • The language of all string not containing the substring 000 (1+0)*10*+ (1+0)*0*1+ (1+0)* • The language of all string that do not contain substring 110 0*1(1+0)*+10*(1+0)*+ (1+0)* • The language of all strings containing both 101 & 010 as substring 1*(1+0)*1*0* • The language of all strings in which both the number of 0’s and the number of 1’s are Even (11+00+ (10+01)(11+00)*(10+01))* • The language of all strings in which both the number of 0’s and the number of 1’s are Odd (11+00+ (10+01)(11+00)*(10+01))*(1+0) • All words that contain Exactly two1’s or three 1’s,not more 11(1+0)*0*+0*1(1+0)*1 • All string that have exactly one double letter in them (1* 01*01*)+ (0*10*10*) • All the word in which 1 is tripled or 0 is tripled , but not both (1* 01*01*01*) + (0*10*10*10*) • All the String in which the total no of 1’s is divisible by 3,no matter how they are arrange such as 1101100101 ((0*10*10*10*))* • All the words in which any 0’s that occurs are found in clumps of odd number at a time , such as 1101100010 1*(00+01)*0 (1)*
  • 40. Important R.E. [Week#03] (a) - Regular Expressions (Examples) • Write a R.E for set of strings over {a,b} that do not contain the substring aa • A string in this set may contain a prefix of any number of b’s. • All a’s must be followed by at least one b or terminate the string. • The R.E b*(ab+) + b*(ab+)*a generates the desired set. • => • b*(ab+)*(Λ+ a) = b*(abb*)*(Λ + a) = (b + ab)*(Λ + a)
  • 41. Regular Expression cont… [Week#03] (a) - Regular Expressions (Examples) Note: Regular expressions describe regular languages Example: (a + bc) * = {, a , bc , aa , abc , bca , ….} // Total Examples Defined over {0,1} = 40
  • 42. Regular Expression cont… Example: (Solved the Un-Solved) [Week#03] (a) - Regular Expressions (Examples) 1. 01* = {0, 01, 011, 0111, …..} 2. (01*)(01) = {001, 0101, 01101, 011101, …..} 3. (0+1)* 4. (0+1)*01(0+1)* 5. ((0+1)(0+1)+(0+1)(0+1)(0+1))* 6. ((0+1)(0+1))*+((0+1)(0+1)(0+1))* 7. (1+01+001)*(Λ+0+00)
  • 43. How to make and check Regular Expression Easily? [Week#03] (a) - Regular Expressions (Examples) Regular Expression cont…
  • 44. Regular Expression cont… Example [Week#03] (a) - Regular Expressions (Examples) Regular expression:   * a b a       * a b a L         * a L b a L       * a L b a L            * a L b L a L            * a b a      ,... , , , , aaa aa a b a     ,... , , ,..., , , baa ba b aaa aa a 
  • 45. Exercise: [Week#03] (a) - Regular Expressions (Examples) • Practice the Examples of Regular Expressions where Σ={ i , j , k }

Editor's Notes

  1. The null string does not belong to all words stating with a, while it does belong to all words not beginning with b It is the fact that it can’t be started with b but it can be started either with a or with null
  2. Note: (a+b)(a+b)*=(a+b)+
  3. ODD-ODD (Σ = {a, b}) i.e. = {a,b,ab,ba,aaa,bbb,abbb,aaabbbbb, …} RE sets: R1 = (a+b)((a+b)(a+b))* R2 = (ab+ba)((ab+ba)(ab+ba))* => R.E. For ODD-ODD = ( (a+b)( (a+b)(a+b) )* + (ab+ba)( (ab+ba)(ab+ba)* ) )
  4. The language of the strings defined over Σ = {a,b}, which do not contain a double letter b(ab)*a Because, there are total 4 letters, with length=2, aa,ab,ba,bb and there are only two double letters, i.e. aa,bb….. We will only focus on the those letters which do not contain a double letter, i.e. ab,ba These double letters can be generated by (ab)*
  5. Means that I am not sure about it…..There is a doubt in the Regular expression
  6. Note: (a+b)(a+b)* = (a+b)+
  7. ? Think…Do it Yourself… (a + b)(aa + bb + ab + ba)*
  8. Note the Difference b/w, umsa ab* = {a, ab,abb,abbb,abbbb, …} (ab)* = {Λ, ab, abab, ababab, …}
  9. |w| means ‘Length of w’
  10. The language of all strings not containing the substring 000. (1+0)+(1+0)(1+0)+(111+110+101+100+011+010+001)( The language of all strings that donot contain the substring 110. (1+0)+(1+0)(1+0)+(111+101+100+011+010+001+000)( The language of all string containing both 101 and 010 as substring. 101 010 + 010101 The language of all strings in which both the numbers of 0’s and 1’s are even. The language of all strings in which both the numbers of 0’s and 1’s are odd. (1+0) All the words that contain exactly two 1’s or three 1’s, not more. + All the strings that have exactly double letters in them. + All words in which 1 is triple or 0 is triple, but not both. This means each word contains 111 or 000 but not both . 111 + All strings in which the total number of 1’s is divisible by 3 no matter how they are distributed, such 1101100101. (1*01*01*01)* All words in which any 0’s that occur are found in clumps of odd number at a time, such as 1101100010. (0(00)*)* (1(11)*) (0(00)*)*