SlideShare a Scribd company logo
1 of 13
REGULAR EXPRESSIONS
DEFINITION
• Regexes (regex) are sequences of characters describing special
search patterns as text strings.
• The basic purpose of them is to match loosely defined patterns to
accomplish some tasks in the programming world.
• Based on a pattern definition, the regex retrieves the strings based
on the exact sequence of characters.
ADVANTAGES
• It helps in important user information validations like email address,
phone numbers and IP address.
• It helps in identifying specific template tags and replacing those
data with the actual data as per the requirement.
• It helps in searching specific string pattern and extracting matching
results in a flexible manner.
• It offers a powerful tool for analyzing, searching a pattern and
modifying the text data.
OPERATORS
^ It denotes the start of string.
$ It denotes the end of string.
. It denotes almost any single
character.
() It denotes a group of
expressions.
[] It finds a range of characters for
example [xyz] means x, y or z .
[^] It finds the items which are not in
range for example [^abc] means
NOT a, b or c.
– (dash) It finds for character range within
the given item range for example
[a-z] means a through z.
| (pipe) It is the logical OR for example x |
y means x OR y.
? It denotes zero or one of
preceding character or item
range.
* It denotes zero or more of
preceding character or item
range.
+ It denotes one or more of
preceding character or item
range.
{n} It denotes exactly n times of
preceding character or item range
for example n{2}.
{n, m} It denotes atleast n but not more
than m times for example n{2, 4}
means 2 to 4 of n.
PREDEFINED FUNCTIONS/ REGEX
LIBRARY
• PHP provides the programmers to many useful
functions to work with regular expressions.
Preg_match_all () -> This function searches for a specific pattern
against some string. It returns true if pattern exists and false otherwise.
Eg.,
<?php (or) <?php
$exp = “ Cap and Map”; $exp = “ Bananas”;
$new= “api”; $new = “/pBa(na){2}/i”;
echo “preg_match_all($new, $exp)”; echo “preg_match_all($new,$exp)”;
?> ?>
<?php
$pattern = "/ca[kf]e/";
$text = "He was eating cake in the cafe.";
$matches = preg_match_all($pattern, $text, $array); echo $matches . "
matches were found.";
?>
O/P: 2 matches were found.
<?php
$pattern = "/s/";
$replacement = "-";
$text = "Earth revolves aroundnthetSun";
// Replace spaces, newlines and tabs
echo preg_replace($pattern, $replacement, $text);
echo "<br>";
// Replace only spaces
echo str_replace(" ", "-", $text);
?>
O/P :
Earth-revolves-around-the-Sun
Earth-revolves-around the Sun
preg_match_all() -> This function searches for all the occurrences of string
pattern against the string. This function is very useful for search and replace.
Eg.,
<?php
$exp = ‘/^[a-z A-Z]*$/’;
$myname = “Kumar”;
if(preg_match($exp,$myname)); {
echo “ Match found”;
}
else {
echo “ Match not found”; }
?>
preg_replace() ->This function behaves like ereg_replace() function
provided the regular expressions can be used in the pattern and replacement
strings.
Eg.,
<?php
$exp = “([0-9]+)”;
$car = “ i brought thuis BMW car on 2020”;
$replace = “2022”;
$car = preg_replace($exp, $replace, $car);
echo $car;
?>
preg_split() -> The function behaves like the PHP split() function. It
splits the string by regular expressions as its parameters.
<?
$IP = "123.456.789.94";
$exp = "/./";
$new = preg_split($exp, $IP);
echo "$new[0] <br>";
echo "$new[1] <br>";
echo "$new[2] <br>";
echo "$new[3] <br>";
?>

More Related Content

Similar to Unit 2 - Regular Expression.pptx

Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdfGaneshRaghu4
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeProf. Wim Van Criekinge
 
RegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing ExamplesRegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing Exampleszeteo12
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle databaseAmrit Kaur
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsDanny Bryant
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)FrescatiStory
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
9780538745840 ppt ch03
9780538745840 ppt ch039780538745840 ppt ch03
9780538745840 ppt ch03Terry Yoast
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptxPadreBhoj
 

Similar to Unit 2 - Regular Expression.pptx (20)

Perl_Part4
Perl_Part4Perl_Part4
Perl_Part4
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
 
RegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing ExamplesRegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing Examples
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
 
RegEx Parsing
RegEx ParsingRegEx Parsing
RegEx Parsing
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
9780538745840 ppt ch03
9780538745840 ppt ch039780538745840 ppt ch03
9780538745840 ppt ch03
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 

Unit 2 - Regular Expression.pptx

  • 2. DEFINITION • Regexes (regex) are sequences of characters describing special search patterns as text strings. • The basic purpose of them is to match loosely defined patterns to accomplish some tasks in the programming world. • Based on a pattern definition, the regex retrieves the strings based on the exact sequence of characters.
  • 3. ADVANTAGES • It helps in important user information validations like email address, phone numbers and IP address. • It helps in identifying specific template tags and replacing those data with the actual data as per the requirement. • It helps in searching specific string pattern and extracting matching results in a flexible manner. • It offers a powerful tool for analyzing, searching a pattern and modifying the text data.
  • 4. OPERATORS ^ It denotes the start of string. $ It denotes the end of string. . It denotes almost any single character. () It denotes a group of expressions. [] It finds a range of characters for example [xyz] means x, y or z .
  • 5. [^] It finds the items which are not in range for example [^abc] means NOT a, b or c. – (dash) It finds for character range within the given item range for example [a-z] means a through z. | (pipe) It is the logical OR for example x | y means x OR y. ? It denotes zero or one of preceding character or item range.
  • 6. * It denotes zero or more of preceding character or item range. + It denotes one or more of preceding character or item range. {n} It denotes exactly n times of preceding character or item range for example n{2}. {n, m} It denotes atleast n but not more than m times for example n{2, 4} means 2 to 4 of n.
  • 7. PREDEFINED FUNCTIONS/ REGEX LIBRARY • PHP provides the programmers to many useful functions to work with regular expressions.
  • 8. Preg_match_all () -> This function searches for a specific pattern against some string. It returns true if pattern exists and false otherwise. Eg., <?php (or) <?php $exp = “ Cap and Map”; $exp = “ Bananas”; $new= “api”; $new = “/pBa(na){2}/i”; echo “preg_match_all($new, $exp)”; echo “preg_match_all($new,$exp)”; ?> ?>
  • 9. <?php $pattern = "/ca[kf]e/"; $text = "He was eating cake in the cafe."; $matches = preg_match_all($pattern, $text, $array); echo $matches . " matches were found."; ?> O/P: 2 matches were found.
  • 10. <?php $pattern = "/s/"; $replacement = "-"; $text = "Earth revolves aroundnthetSun"; // Replace spaces, newlines and tabs echo preg_replace($pattern, $replacement, $text); echo "<br>"; // Replace only spaces echo str_replace(" ", "-", $text); ?> O/P : Earth-revolves-around-the-Sun Earth-revolves-around the Sun
  • 11. preg_match_all() -> This function searches for all the occurrences of string pattern against the string. This function is very useful for search and replace. Eg., <?php $exp = ‘/^[a-z A-Z]*$/’; $myname = “Kumar”; if(preg_match($exp,$myname)); { echo “ Match found”; } else { echo “ Match not found”; } ?>
  • 12. preg_replace() ->This function behaves like ereg_replace() function provided the regular expressions can be used in the pattern and replacement strings. Eg., <?php $exp = “([0-9]+)”; $car = “ i brought thuis BMW car on 2020”; $replace = “2022”; $car = preg_replace($exp, $replace, $car); echo $car; ?>
  • 13. preg_split() -> The function behaves like the PHP split() function. It splits the string by regular expressions as its parameters. <? $IP = "123.456.789.94"; $exp = "/./"; $new = preg_split($exp, $IP); echo "$new[0] <br>"; echo "$new[1] <br>"; echo "$new[2] <br>"; echo "$new[3] <br>"; ?>