Upload
susanna-carr
View
219
Download
0
Embed Size (px)
Citation preview
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
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, …. }
Regular Expressions used in
APPLICATIONS LANGUAGES
Apache HTTP Server .NETMicrosoft Visual Studio C++Netbeans DNotepad++ JavaLibreOffice Calc JavaScriptOracle Database PerlPython PHPSublime Text Ruby
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
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’!
<?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:
<?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.,
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
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');
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
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
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
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.
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/
Thank You
AnyQueries???