8/3/2019 Lec2 Regular Expression
1/30
CSC 416
Lecture 2egu ar express on
8/3/2019 Lec2 Regular Expression
2/30
Regular Expressions A RE is a lan ua e for describin sim le lan ua es
and patterns.
Algorithm for using RE
1. Define a pattern: S1StudentnameS2
2. Loo
Find next patternStore StudentName into DB or encr t StudentName
Until no match
REs are used in a lications that involve file arsinand text matching.
Many Implementations have been made for RE.
Dr.Hussien Sharaf 2
8/3/2019 Lec2 Regular Expression
3/30
y oos rary //Pattern matching in a String < >. . // distrubuted under GPL v2 license
#include #include Please check
int main() {boost::regex pattern" "
[http://flavio.castelli.name/regexp-with-boost]
, _boost::regex_constants::perl);std::string stringa ("Searching for BsLug");
if (boost::regex_search (stringa, pattern,
boost::regex_constants::format_perl))printf ("found\n");else printf("not found\n");
Dr.Hussien Sharaf 3
8/3/2019 Lec2 Regular Expression
4/30
y oos rary//Substitution// Created by Flavio Castelli
#include
#include int main() Please check{boost::regex pattern
("bok",boost::regex_constants::icase|
[http://flavio.castelli.name/regexp-with-boost]
oos ::regex_cons an s::per ;std::string stringa ("Searching for bok");
"
std::string newString;
newString = boost::regex_replace (stringa,pattern, replace);
printf("The new string is:", . _
return 0; }Dr.Hussien Sharaf 4
8/3/2019 Lec2 Regular Expression
5/30
RE by C++ STLneeds extra feature pack#include
#include
#include
bool is_email_valid(const std::string& email) {// define a regular expression
const std::tr1::regexpattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
try to matc t e str ng w t t e regu ar express on
return std::tr1::regex_match(email, pattern); }
Please check
Dr.Hussien Sharaf 5
p: www.co eguru.com cpp cpp cpp_m c s ar c e.p p c
8/3/2019 Lec2 Regular Expression
6/30
RE by C++ STL#include
#include
#include
bool is_email_valid(const std::string& email) {// define a regular expression
const std::tr1::regexpattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
try to matc t e str ng w t t e regu ar express on
return std::tr1::regex_match(email, pattern);}
Please check
Dr.Hussien Sharaf 6
p: www.co eguru.com cpp cpp cpp_m c s ar c e.p p c
8/3/2019 Lec2 Regular Expression
7/30
RE by C++ STLstd::strin str "abc + inside brackets dfsd"
std::smatch m;
std::re ex search str m std::re ex b"_
if (m[0].matched)
8/3/2019 Lec2 Regular Expression
8/30
Other code samples Other sam les can be check at: Boost Library
1. http://stackoverflow.com/questions/5804453/c-regular-- - -
2. http://www.codeproject.com/KB/string/regex__.aspx
3. http://www.boost.org/doc/libs/1_43_0/more/getting_started/w
indows.html#build-from-the-visual-studio-ide STL Library
1. htt : www.code ro ect.com KB reci es rexsearch.as x
2. http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339 [recommended]
. . . .6922 [feature pack VS2008 Pack]
4. http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-ep an- - avave - an ar - emp a e- rary- - -o -n eo
Dr.Hussien Sharaf 8
8/3/2019 Lec2 Regular Expression
9/30
RE Rules -1= = *. ,
In RE, we write x
*
*. e = x, y , en =
In RE, we write (x+y)*
3. Kleenes star * means any combination of lettersof length zero or more.
Dr.Hussien Sharaf 9
8/3/2019 Lec2 Regular Expression
10/30
RE Rules -2,
by the following rules.
1. For every letter in , the letter written in bold is a regularexpression. is a regular expression.
2. Ifr1 and r2 are regular expressions, then so are:
1. r1
2. r1 r23. r +r
4. r1*
NOTE: r1+
is not a RE3. Nothing else is a regular expression.
Dr.Hussien Sharaf 10
8/3/2019 Lec2 Regular Expression
11/30
RE-1
={a, b}- orma y escr e a wor s w a o owe y any
number of bs
*
*
= a = a- Give examples for words in L
{a ab abb abbb ..}
Dr.Hussien Sharaf 11
8/3/2019 Lec2 Regular Expression
12/30
RE-2
={a, b}- orma y escr e e anguage a con a ns
nothing and contains words where any a must be
L = (abb*)*
- ve examp es or wor s n
{ ab abb abababb ..}
Dr.Hussien Sharaf 12
8/3/2019 Lec2 Regular Expression
13/30
RE-3
={a, b}- orma y escr e a wor s w a o owe y one
or more bs
*
= a = a- Give examples for words in L
{ab abb abbb ..}
Dr.Hussien Sharaf 13
8/3/2019 Lec2 Regular Expression
14/30
RE-4
={a, b, c} - orma y escr e a wor s a s ar w an afollowed by any number of bs and then end with c.
*
= a c- Give examples for words in L
{ac abc abbc abbbc ..}
Dr.Hussien Sharaf 14
8/3/2019 Lec2 Regular Expression
15/30
RE-5
={a, b} - orma y escr e a wor s w ere a s any comebefore bs if any.
* *
= a- Give examples for words in L
{ a b aa ab bb aaa abb abbb bbb..}
NOTE: a*
b*
(ab)*
because first language does not contain abab but second language has.
nce s ng e s e ec e en no a s can e a e
Dr.Hussien Sharaf 15
8/3/2019 Lec2 Regular Expression
16/30
RE-6
={a} - orma y escr e a wor s w ere coun o a s o .
L = a(aa)* OR (aa)*a
- Give examples for words in L{a aaa aaaaa ..}
Dr.Hussien Sharaf 16
8/3/2019 Lec2 Regular Expression
17/30
RE-7.1
={a, b, c} - orma y escr e a wor s w ere s ng e a or ccomes in the start then odd number of bs.
*
= a+c- Give examples for words in L
{ab cb abbb cbbb ..}
Dr.Hussien Sharaf 17
8/3/2019 Lec2 Regular Expression
18/30
RE-7.2
={a, b, c} - orma y escr e a wor s w ere s ng e a or ccomes in the start then odd number of bs in case of a
.
L = ab(bb)* +c(bb)*
- ve examp es or wor s n
{ab c abbb cbb abbbbb ..}
Dr.Hussien Sharaf 18
8/3/2019 Lec2 Regular Expression
19/30
RE-8
={a, b, c} - orma y escr e a wor s w ere one or more a orone or more c comes in the start then one or more
.
L = (a+c) + b+= (aa*+cc*) bb*
- ve examp es or wor s n
{ab cb aabb cbbb ..}
Dr.Hussien Sharaf 19
8/3/2019 Lec2 Regular Expression
20/30
RE-9
={a, b} - orma y escr e a wor s w eng ree.
L = (a+b) 3 =(a+b) (a+b) (a+b)
- List all words in L{aaa aab aba baa abb bab bba bbb}
- What is the count of words of length 4?
16 = 24
- What is the count of words of length 44?44
Dr.Hussien Sharaf 20
8/3/2019 Lec2 Regular Expression
21/30
Dr.Hussien Sharaf 21
8/3/2019 Lec2 Regular Expression
22/30
RE-10.1
={a, b}, What does L describe?
* *- = a+ a a+
ny s r ng o a s an s
Single aAny string of a's and b's
- Give examples for words in L
{a ab aab bab abb ..}
Dr.Hussien Sharaf 22
8/3/2019 Lec2 Regular Expression
23/30
RE-10.2
a aa
abb a ab
a a a
abba a b
Dr.Hussien Sharaf 23
8/3/2019 Lec2 Regular Expression
24/30
-
={a, b}
- orma y escr e a wor s w a eas wo a s.
1) L = b*ab*a(a + b)*
Start with a jungle of b's (or no b's) until we findthe first a, then more b's (or no b's), then thesecond a, then we finish up with anything.
- {abbbabb aaaaa bbbabbbbabab..}
Dr.Hussien Sharaf 24
8/3/2019 Lec2 Regular Expression
25/30
-
={a, b}
- orma y escr e a wor s w exac y wo a s.
1) L = b*ab*ab*
- Give examples for words in L
To make the word aab, we let the first and second b*
Dr.Hussien Sharaf 25
8/3/2019 Lec2 Regular Expression
26/30
RE-13.1
={a, b}
- orma y escri e a wor s wit east one a an east
one b.
1) L = (a + b)*a(a + b)* b(a + b)*= an thin a an thin b an thin
But (a+b)*a(a+b)*b(a+b)* expresses all words except
some as (at least one). bb*aa*
Dr.Hussien Sharaf 26
8/3/2019 Lec2 Regular Expression
27/30
RE-13.2= * * * * *
Thus: (a+b)*a(a+b)*b(a+b)* + (a+b)*b(a+b)*a(a+b)*
= * * * * *
Notice that it is necessary to write bb*aa* because* * , .
oes s mp y a
(a+b)*b(a+b)*a(a+b)*= bb*aa*??False Left side includes the word aba, which the
ex ression on the ri ht side does not.
Dr.Hussien Sharaf 27
8/3/2019 Lec2 Regular Expression
28/30
egu ar anguages A lan ua e that can be defined b a RE is called a
regular language.
n n can no express some anguages suc as .1. If L1 and L2 are regular languages then
L1 + L2 , L1 L2 and L1 are also regular languages.2. If L is a re ular lan ua e then L L com lement
is also a regular language.
is the RE that accepts language L.
= =
Dr.Hussien Sharaf 28
8/3/2019 Lec2 Regular Expression
29/30
Regular Languages 2. 1 2
L1 L
2 is also a regular language. L1 =words starting with a
r =a a+b *
L2 =words ending with a*2
L3 =L1 L2 = a(a+b)*a L3 = words that start and end with a.
Dr.Hussien Sharaf 29
8/3/2019 Lec2 Regular Expression
30/30
Dr.Hussien Sharaf 30
Top Related