0% found this document useful (0 votes)
8 views

Regular Expression

The document provides a comprehensive overview of regular expressions (regex), including their definitions, properties, and examples. It explains how regex can represent regular languages and outlines various operations such as union, intersection, and concatenation. Additionally, it includes exercises for constructing non-deterministic finite automata (NFA) from regular expressions and writing regex for specific string patterns.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Regular Expression

The document provides a comprehensive overview of regular expressions (regex), including their definitions, properties, and examples. It explains how regex can represent regular languages and outlines various operations such as union, intersection, and concatenation. Additionally, it includes exercises for constructing non-deterministic finite automata (NFA) from regular expressions and writing regex for specific string patterns.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Regular Expression

A method of representing language as expression is known as “regular


expression”.

That is, the language accepted by finite state automata is easily


described by simple expressions.
A regular expression is basically a shorthand way of showing how a regular
language is built from the base set of regular languages.

A Regular Expression (or Regex) is a pattern (or filter) that describes a set of
strings that matches the pattern.
A Regular Expression can be recursively defined as follows −

• ε is a Regular Expression indicates the language containing an empty string. (L (ε) = {ε})

• φ is a Regular Expression denoting an empty language. (L (φ) = { })

• x is a Regular Expression where L = {x}

• If X is a Regular Expression denoting the language L(X) and Y is a Regular Expression


denoting the language L(Y), then

• X + Y is a Regular Expression corresponding to the language L(X) ∪ L(Y) where


L(X+Y) = L(X) ∪ L(Y).

• X . Y is a Regular Expression corresponding to the language L(X) . L(Y) where


L(X.Y) = L(X) . L(Y)

• R* is a Regular Expression corresponding to the language L(R*)where L(R*) =


(L(R))*

If we apply any of the rules several times, they are Regular Expressions.
Regular Expressions Regular Set
Some RE Examples

(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }


(0*10*) L = {1, 01, 10, 010, 0010, …}
(0 + ε)(1 + ε) L = {ε, 0, 1, 01}
Set of strings of a’s and b’s of any length inluding the null
(a+b)* string. So L = { ε, a, b, aa , ab , bb , ba, aaa…….}
Set of strings of a’s and b’s ending with the string abb. So L
(a+b)*abb
= {abb, aabb, babb, aaabb, ababb, …………..}
Set consisting of even number of 1’s including empty
(11)*
string, So L= {ε, 11, 1111, 111111, ……….}
Set of strings consisting of even number of a’s followed by
(aa)*(bb)*b odd number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab,
aaaabbb, …………..}
String of a’s and b’s of even length can be obtained by
(aa + ab + ba + bb)* concatenating any combination of the strings aa, ab, ba
and bb including null, so L = {aa, ab, ba, bb, aaab, aaba,
…………..}
Regular Sets

Any set that represents the value of the Regular Expression is called a Regular Set.

Properties of Regular Sets


Property 1. The union of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(aa)* and RE2 = (aa)*
So, L1 = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)
and L2 ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}
(Strings of all possible lengths including Null)

RE (L1 ∪ L2) = a* (which is a regular expression itself)


Hence, proved.
Property 2. The intersection of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(a*) and RE2 = (aa)*
So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths
excluding Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length
including Null)
L1 ∩ L2 = { aa, aaaa, aaaaaa,.......} (Strings of even length
excluding Null)
RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.
Hence, proved.
Property 3. The complement of a regular set is
regular.
Proof −
Let us take a regular expression −
RE = (aa)*
So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even
length including Null)
Complement of L is all the strings that is not in L.
So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length
excluding Null)
RE (L’) = a(aa)* which is a regular expression itself.
Hence, proved.
Property 4. The difference of two regular set is
regular.
Proof −
Let us take two regular expressions −
RE1 = a (a*) and RE2 = (aa)*
So, L1 = {a, aa, aaa, aaaa, ....} (Strings of all possible
lengths excluding Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even
length including Null)
L1 – L2 = {a, aaa, aaaaa, aaaaaaa, ....}
(Strings of all odd lengths excluding Null)
RE (L1 – L2) = a (aa)* which is a regular expression.
Property 5. The reversal of a regular set is
regular.
Proof −
We have to prove LR is also regular if L is a
regular set.
Let, L = {01, 10, 11, 10}
RE (L) = 01 + 10 + 11 + 10
LR = {10, 01, 11, 01}
RE (LR) = 01 + 10 + 11 + 10 which is regular
Hence, proved.
Property 6. The closure of a regular set is regular.
Proof −
If L = {a, aaa, aaaaa, .......} (Strings of odd length
excluding Null)
i.e., RE (L) = a (aa)*
L* = {a, aa, aaa, aaaa , aaaaa,……………} (Strings of all
lengths excluding Null)
RE (L*) = a (a)*
Hence, proved.
Property 7. The concatenation of two regular sets is regular.
Proof −
Let RE1 = (0+1)*0 and RE2 = 01(0+1)*
Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)

and L2 = {01, 010,011,.....} (Set of strings beginning with 01)

Then, L1 L2 =
{001,0010,0011,0001,00010,00011,1001,10010,.............}

Set of strings containing 001 as a substring which can be


represented by an RE − (0 + 1)*001(0 + 1)*
Hence, proved.
Write a regular expression for the language accepting all combination of a’s over the set ∑ =
{a}.

All combinations of a’s means a may be single, double, triple and so on.
There may be the case that ‘a’ is appearing for zero times, which means a null string.
So L={ε,a,aa,aaa,aaaa,...}. R=a*

Design the regular expression for the language accepting all combinations of a’s except the
null string over ∑={a}.

R=a+

Design the regular expression for the language containing all the strings containing any
number of a’s and b’s.

Solution: R = (a+b)*
Construct the regular expression for the language accepting all the strings which are
ending with 00 over the set ∑={0,1}.

Solution: R = (0+1)*00

Write the regular expression for the language accepting the string which are starting
with 1 and ending with 0 over the set ∑={0,1}.

Solution: R = 1(0+1)*0

Describe in simple English the language represented by the following regular


expression r= (a+ab)*

Solution: L(r) = {a,ab,aab,abab,.....}


Identities Related to Regular Expressions
Given R, P, L, Q as regular expressions, the following identities hold −
• ∅* = ε
• ε* = ε
• RR* = R*R
• R*R* = R*
• (R*)* = R*
• RR* = R*R
• (PQ)*P =P(QP)*
• (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
• R + ∅ = ∅ + R = R (The identity for union)
• R ε = ε R = R (The identity for concatenation)
• ∅ L = L ∅ = ∅ (The annihilator for concatenation)
• R + R = R (Idempotent law)
• L (M + N) = LM + LN (Left distributive law)
• (M + N) L = ML + NL (Right distributive law)
• ε + RR* = ε + R*R = R*
Finite Automata and Regular Expressions
Construction of NFA from Regular expression

Example 1: Construct a NFA for the regular expression 01.

Example 2: Construct a NFA for the regular expression a*


Example 3: Construct a NFA for the regular expression a+ .

Example 4: Construct a NFA for the regular expression ba+ .


Example 5: Construct a NFA for the regular expression ba* .

Example 6: Construct a NFA for the regular expression b+ba*


Example 7: Construct a NFA for the regular expression (0+1)*.
Exercise Problems:
Construct a NFA for the following regular expressions:

i) R = (01+10)* or (01/10)* or (01 ꓴ 10)*


ii) R = 0(00)*
iii) R = (01+2*)1
iv) R = (a/b)*abb
v) R = (a*/b*)*
Vi) R = ((ϵ/a)b*)*
Vii) R = ((a/b)*)abb(a/b)*
viii) R = (a(aa)*b+ab*a)*
Write Regular Expression for all strings over {0,1} with the substring ‘0101’
(0/1)*0101(0/1)*

Write Regular Expression for all strings beginning with ’11 ‘ and ending with ‘ab’
11 (1+a+b)*ab

Write regular expressions for the set of a’s and b’s with at most one pair of
consecutive a’s.
(b + ab)* (aa + ɛ)(b + ab)*

Write regular expression for the language that have the set of all strings of 0’s and
1’s whose 10th symbol from the right end is 1.
(0 + 1)* 1(0 + 1)9

Write Regular Expression for the Set of all strings over {a,b}with 3 consecutive b’s.
(a/b)*bbb(a/b)*

Write Regular Expression for the Set of all strings that end with ‘1’and has no
substring ‘00’
(01+1)(01+1)*
Write Regular Expression for the language that have the set of all
strings of 0’s and 1’s such that every pair of adjacent 0’s appears
before any pair of adjacent 1’s.
R(L)=(0+10)*(1+01)*(0+ϵ).

Write Regular Expression for the language that has the set of all
strings of 0’s and 1’s not containing 101 as a substring
((0* 11*0). 0)* 1 (0/1)*

Write regular expressions for the set of strings of 0’s and 1’s whose
number of 0’s is divisible by 5.
(1*01*01*01*01*01*)*

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy