Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

ASSIGNMENT/ASSESSMENT COVER SHEET

Student Name: Imran Hakeem

FIRST NAME FAMILY / LAST NAME

Registration Number: BSCS-023R13-191

Course Code Course Title

Theory of Autamata

Department (Computer Science / Information Technology)

Computer Science

Class & Semester BSCS 7th

Assessment Item Title: 03 Due Date/Time: 29-04-20


Due Date/Time 06-05-20:

Tutorial Group (If applicable): Word Count (If applicable):


Lecturer/Tutor Name: Zia Ur Rehman

Extension Granted: Yes No Granted By:

I declare that this assessment item is my own work unless otherwise acknowledged and is
in accordance with the University’s academic integrity policy.
I certify that this assessment item has not been submitted previously for academic credit in
this or any other course. I certify that I have not given a copy or have shown a copy of this
assessment item to another student enrolled in the course.
I acknowledge that the assessor of this assignment may, for the purpose of assessing this
assignment:
 Reproduce this assessment item and provide a copy to another member of the Faculty; and/or
 Communicate a copy of this assessment item to a plagiarism checking service (which may then
retain a copy of the item on its database for the purpose of future plagiarism checking).
 Submit the assessment item to other forms of plagiarism checking.

I certify that any electronic version of this assessment item that I have submitted or will
submit is identical to this paper version.

Signature: Imran Hakeem Date: 20-08-20

Using twenty examples explain regular expression?


A regular expression defines a search pattern for strings. The abbreviation for regular expression
is regex. The search pattern can be anything from a simple character, a fixed string or a complex
expression containing special characters describing the pattern. The pattern defined by the regex
may match one or several times or not at all for a given string.

Regular expressions can be used to search, edit and manipulate text.

The process of analyzing or modifying a text with a regex is called: The regular expression is
applied to the text/string. The pattern defined by the regex is applied on the text from left to
right. Once a source character has been used in a match, it cannot be reused. For example, the
regex aba will match ababababa only two times (aba_aba__).

A regular expression (shortened as regex or regexp also referred to as rational expression) is a


sequence of characters that define a search pattern. Usually such patterns are used by string-
searching algorithms for "find" or "find and replace" operations on strings, or for input
validation. It is a technique developed in theoretical computer science and formal
language theory.

The concept arose in the 1950s when the American mathematician Stephen Cole
Kleene formalized the description of a regular language. The concept came into common use
with Unix text-processing utilities. Different syntaxes for writing regular expressions have
existed since the 1980s, one being the POSIX standard and another, widely used, being
the Perl syntax.

Regular expressions are used in search engines, search and replace dialogs of word
processors and text editors, in text processing utilities such as sed and AWK and in lexical
analysis. Many programming languages provide regex capabilities either built-in or via libraries.

Examples of Regular Expression

Example

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

Solution:

In a regular expression, the first symbol should be 1, and the last symbol should be 0. The r.e. is
as follows:

R = 1 (0+1)* 0  

Example

Write the regular expression for the language starting and ending with a and having any having
any combination of b's in between.

Solution:

The regular expression will be:


R = a b* b  

Example

Write the regular expression for the language starting with a but not having consecutive b's.

Solution: The regular expression has to be built for the language:

L = {a, aba, aab, aba, aaa, abab, .....}  

The regular expression for the above language is:

R = {a + ab}*  

Example

Write the regular expression for the language accepting all the string in which any number of a's
is followed by any number of b's is followed by any number of c's.

Solution: As we know, any number of a's means a* any number of b's means b*, any number of
c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So
the regular expression could be:

R = a* b* c*  

Example

Write the regular expression for the language over ∑ = {0} having even length of the string.

Solution:

The regular expression has to be built for the language:

L = {ε, 00, 0000, 000000, ......}  

The regular expression for the above language is:

R = (00)*  

Example
Write the regular expression for the language having a string which should have atleast one 0 and
alteast one 1.

Solution:

The regular expression will be:

R = [(0 + 1)* 0 (0 + 1)* 1 (0 + 1)*] + [(0 + 1)* 1 (0 + 1)* 0 (0 + 1)*]  

Example

Describe the language denoted by following regular expression

r.e. = (b* (aaa)* b*)*  

Solution:

The language can be predicted from the regular expression by finding the meaning of it. We will
first split the regular expression as:

r.e. = (any combination of b's) (aaa)* (any combination of b's)

L = {The language consists of the string in which a's appear triples, there is no restriction on the
number of b's}

Example

Write the regular expression for the language L over ∑ = {0, 1} such that all the string do not
contain the substring 01.

Solution:

The Language is as follows:

L = {ε, 0, 1, 00, 11, 10, 100, .....}  

The regular expression for the above language is as follows:

R = (1* 0*)  
Example

Write the regular expression for the language containing the string over {0, 1} in which there are
at least two occurrences of 1's between any two occurrences of 1's between any two occurrences
of 0's.

Solution: At least two 1's between two occurrences of 0's can be denoted by (0111*0)*.

Similarly, if there is no occurrence of 0's, then any number of 1's are also allowed. Hence the r.e.
for required language is:

R = (1 + (0111*0))*  

Example

Write the regular expression for the language containing the string in which every 0 is
immediately followed by 11.

Solution:

The regular expectation will be:

R = (011 + 1)*  

Meta-
characte Description Example[51]
r(s)

. Normally matches any character except a newline. $string1 =


Within square brackets the dot is literal. "Hello
World\n";
if ($string1
=~ m/...../) {
print
"$string1 has
length >=
5.\n";
}

Output:
Hello World
has length >=
5.

$string1 =
"Hello
World\n";
if ($string1
=~ m/(H..).
(o..)/) {
print "We
Groups a series of pattern elements to a single element.
matched '$1'
( ) When you match a pattern within parentheses, you can use any and '$2'.\n";
of  $1 ,  $2 , ... later to refer to the previously matched pattern. }

Output:

We matched
'Hel' and 'o
W'.

$string1 =
"Hello
World\n";
if ($string1
=~ m/l+/) {
print "There
are one or
more
consecutive
letter \"l\"'s
in
+ Matches the preceding pattern element one or more times. $string1.\n";
}

Output:

There are one


or more
consecutive
letter "l"'s
in Hello
World.

? Matches the preceding pattern element zero or one time.


$string1 =
"Hello
World\n";
if ($string1
=~ m/H.?e/) {
print "There
is an 'H' and
a 'e'
separated by
";
print "0-1
characters
(e.g., He Hue
Hee).\n";
}

Output:

There is an
'H' and a 'e'
separated by
0-1 characters
(e.g., He Hue
Hee).

? Modifies the  * ,  + ,  ?  or  {M,N} 'd regex that comes before to


$string1 =
match as few times as possible.
"Hello
World\n";
if ($string1
=~ m/(l.+?o)/)
{
print "The
non-greedy
match with 'l'
followed by
one or ";
print "more
characters is
'llo' rather
than 'llo
Wo'.\n";
}

Output:

The non-greedy
match with 'l'
followed by
one or more
characters is
'llo' rather
than 'llo Wo'.

$string1 =
"Hello
World\n";
if ($string1
=~ m/el*o/) {
print "There
is an 'e'
followed by
zero to many
";
print "'l'
followed by
'o' (e.g., eo,
* Matches the preceding pattern element zero or more times. elo, ello,
elllo).\n";
}

Output:

There is an
'e' followed
by zero to
many 'l'
followed by
'o' (e.g., eo,
elo, ello,
elllo).

{M,N} Denotes the minimum M and the maximum N match count.


N can be omitted and M can be 0:  {M}  matches "exactly" M $string1 =
"Hello
times;  {M,}  matches "at least" M times;  {0,N}  matches "at most" World\n";
N times. if ($string1
=~ m/l{1,2}/)
x* y+ z?  is thus equivalent to  x{0,} y{1,} z{0,1} .
{
print "There
exists a
substring with
at least 1 ";
print "and
at most 2 l's
in
$string1\n";
}

Output:
There exists a
substring with
at least 1 and
at most 2 l's
in Hello World

$string1 =
"Hello
World\n";
if ($string1
=~ m/[aeiou]
+/) {
print
"$string1
contains one
[…] Denotes a set of possible character matches. or more
vowels.\n";
}

Output:

Hello World
contains one
or more
vowels.

$string1 =
"Hello
World\n";
if ($string1
=~ m/(Hello|
Hi|Pogo)/) {
print
"$string1
contains at
least one of
| Separates alternate possibilities. Hello, Hi, or
Pogo.";
}

Output:

Hello World
contains at
least one of
Hello, Hi, or
Pogo.
$string1 =
"Hello
World\n";
if ($string1
=~ m/llo\b/) {
print "There
Matches a zero-width boundary between a word-class character is a word that
(see next) and either a non-word class character or an edge; same ends with
\b as 'llo'.\n";
}
(^\w|\w$|\W\w|\w\W) .
Output:

There is a
word that ends
with 'llo'.

$ Matches the end of a line or string.


$string1
= "Hello
World\n"
;
if
($string
1 =~
m/rld$/)
{
print
"$string
1 is a
line or
string
";
print
"that
ends
with
'rld'.\n
";
}

Output:

Hello
World
is a
line or
string
that
ends
with
'rld'.

You might also like