19
MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: [email protected]

MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: [email protected]

Embed Size (px)

Citation preview

Page 1: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

MORE APPLICATIONS OF REGULAR EXPRESSION

ByVenkata Sai Pundamalli

E-mail id: [email protected]

Page 2: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

CONTENTS

Introduction to Regular Expression

Regular Expressions used in

Applications of Regular Expression

a. RE in PHP

b. RE in Databases

c. RE in Voice Recognition

Page 3: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

Regular Expression

A regular expression is an algebraic formula whose value is a pattern consisting of a set of strings.

Example:

The set of strings over {A-Z,a-z} that contain the word “main”<letter> = A | B | ……. | Z | a | b | ……. | z

<letter>* main <letter>*

Here, <letter>* may or may not contain any combination of strings over {A- Z,a-z}

<letter>* = { ε, a , b, aa, bb, ab, bc, ez, abc, main, part, …. }

Page 4: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

Regular Expressions used in

APPLICATIONS LANGUAGES

Apache HTTP Server .NETMicrosoft Visual Studio C++Netbeans DNotepad++ JavaLibreOffice Calc JavaScriptOracle Database PerlPython PHPSublime Text Ruby

Page 5: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

a. Regular Expression in PHP

The ereg() function searches a string to match the given pattern, returning true if the pattern is found, and false otherwise. The search is case sensitive

The eregi() function searches a string to match the given pattern, returning true if the pattern is found, and false otherwise. The search is not case sensitive.

Applications of Regular Expression

Page 6: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

Difference between ereg and eregi:<?php$string = 'XYZ';if (ereg('z', $string)) { echo "'$string' contains a 'z'"; }else{ echo "'$string' contains a 'Z'!";}?>

OUTPUT

'XYZ' contains a 'Z'!

<?php$string = 'XYZ';if (eregi('z', $string)) { echo "'$string' contains a 'z‘ or ‘Z’"; }else{ echo "'$string' contains a 'Z'!";}?>

OUTPUT

'XYZ' contains a ‘z‘ or ‘Z’!

Page 7: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

<?php$string = "abcd";$string1 = ereg("^a",$string);if($string1==1)echo "TRUE";elseecho "FALSE";?>

<?php$string = "abcd";$string1 = ereg("bc$",$string);if($string1==1)echo "TRUE";elseecho "FALSE";?>

Output:TRUE

Output:FALSE

Examples of ereg in PHP:

Page 8: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

<?php$string = "this is sample \nwhich is also sample";echo $string;echo "\nAfter Replacement ::\n";$string = ereg_replace("\n", "", $string);echo $string;?>

Output:

this is sample which is also sampleAfter Replacement ::this is sample which is also sample

<?php$string = "this is sample";$string = ereg_replace("^", "<br />", $string); echo $string;?>

Output:

<br />this is sample

Contd.,

Page 9: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

b. Regular Expression in Databases

Oracle Database implementation of Regular Expressions

SQL Element Description

REGEXP_LIKE It’s a condition used to match the given patternREGEXP_COUNT It’s a function which returns number of times the given

pattern appears in the given stringREGEXP_REPLACE It’s a function which searches for a pattern in a character

column and replace each occurrence of that pattern with the specified string

Page 10: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

For instance,To ensure that phone numbers are entered into the database in a standard format.(XXX) XXX-XXXX

DROP TABLE contacts;CREATE TABLE contacts ( l_name VARCHAR2(30), p_number VARCHAR2(30) CONSTRAINT c_contacts_pnf CHECK (REGEXP_LIKE (p_number, '^\(\d{3}\) \d{3}-\d{4}$')));

INSERT INTO contacts (p_number) VALUES('(650) 555-0100');INSERT INTO contacts (p_number) VALUES('(650)555-0100');

Page 11: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

The following example examines the given string. Oracle returns the number of times that the word ‘the’ appears in the sting.

SELECT REGEXP_COUNT ('The example shows how to use the REGEXP_COUNT function.', 'the', 1, 'i') ”REGEXP_COUNT” FROM dual;

REGEXP_COUNT---------------------------------------2

Page 12: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

The following example examines country_name. Oracle puts a space after each non-null character in the string.

SELECT REGEXP_REPLACE(country_name, '(.)', '\1 ') "REGEXP_REPLACE" FROM countries;

REGEXP_REPLACE------------------------------------------------A r g e n t i n aA u s t r a l i aB e l g i u mB r a z i lC a n a d a

Page 13: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

For each name in the table whose format is "first middle last", we reposition the characters so that the format becomes "last, first middle":

SELECT names "names", REGEXP_REPLACE(names, '^(\S+)\s(\S+)\s(\S+)$', '\3, \1 \2') AS "names after regexp"FROM famous_peopleORDER BY "names";

names names after regexp--------------------------- ---------------------------------Harry S. Truman Truman, Harry S.John Quincy Adams Adams, John QuincyJohn Adams John Adams John Quincy Adams John Quincy AdamsJohn_Quincy_Adams John_Quincy_Adams

Page 14: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

c. Regular Expression in voice recognition

A Speech recognition application is capable of launching external applications based on the command we tell to it.

Regular Expression used in this application is:

string command = Regex.Replace(line, "Start", "").Trim();

This application is written in C# with the help of Visual Studio Express.

Page 15: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu
Page 16: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu
Page 17: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu
Page 18: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

References:

https://www.cs.rochester.edu/~nelson/courses/csc_173/fa/re.html http://www.tutorialspoint.com/php/php_ereg.htm http://docs.oracle.com/cd/E11882_01/appdev.112/e41502/

adfns_regexp.htm#ADFNS00402 http://docs.oracle.com/cd/B28359_01/appdev.111/b28424/

adfns_regexp.htm#CHDJAGEF http://docs.oracle.com/cd/B12037_01/server.101/b10759/functions115.htm http://www.prodigyproductionsllc.com/articles/programming/simple-speech-

recognition-using-c-part-2/

Page 19: MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli E-mail id: vpundama@kent.edu

Thank You

AnyQueries???