MORE APPLICATIONS OF REGULAR EXPRESSION By Venkata Sai Pundamalli id:
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” = A | B | ……. | Z | a | b | ……. | z * main * Here, * may or may not contain any combination of strings over {A- Z,a-z} * = { ε, a, b, aa, bb, ab, bc, ez, abc, main, part, …. }
Regular Expressions used in APPLICATIONSLANGUAGES Apache HTTP Server.NET Microsoft Visual StudioC++ NetbeansD Notepad++Java LibreOffice CalcJavaScript Oracle DatabasePerl PythonPHP Sublime TextRuby
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"; else echo "FALSE"; ?> <?php $string = "abcd"; $string1 = ereg("bc$",$string); if($string1==1) echo "TRUE"; else echo "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 sample After Replacement :: this is sample which is also sample <?php $string = "this is sample"; $string = ereg_replace("^", " ", $string); echo $string; ?> Output: this is sample Contd.,
b. Regular Expression in Databases Oracle Database implementation of Regular Expressions SQL ElementDescription REGEXP_LIKEIt’s a condition used to match the given pattern REGEXP_COUNTIt’s a function which returns number of times the given pattern appears in the given string REGEXP_REPLACEIt’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) ');
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
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 a A u s t r a l i a B e l g i u m B r a z i l C 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_people ORDER BY "names"; names names after regexp Harry S. Truman Truman, Harry S. John Quincy Adams Adams, John Quincy John Adams John Quincy Adams John 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: #ADFNS00402 #CHDJAGEF recognition-using-c-part-2/
Thank You Any Queries???