SlideShare a Scribd company logo
1 of 28
Regular Expression Basics ,[object Object],[object Object]
What are regular expressions? ,[object Object],[object Object]
Regular Expression Basics Literals bus Matches a ‘ b ’, followed by a ‘ u ’, followed by an ‘ s ’
Regular Expression Basics Anchors ^ Matches at the beginning of a line $ Matches at the end of a line
Regular Expression Basics Character Classes [abc] Matches one of ‘ a ’, ‘ b ’ or ‘ c ’ [a-c] Same as above (character range) [^abc] Matches one character that is not listed . Matches any single character
Regular Expression Basics Alternation a|b Matches one of ‘ a ’ or ‘ b ’ dog|cat Matches one of “dog” or “cat”
Regular Expression Basics Quantifiers (repetition) {x,y} Matches minimum of  x  and a maximum of  y  occurrences; either can be omitted * Matches zero or more occurrences (any amount). Same as  {0,} + Matches one or more occurrences. Same as  {1,} ? Matches zero or one occurrences. Same as  {0,1}
Regular Expression Basics Grouping (…) Groups the contents of the parentheses. Affects alternation and quantifiers. Allows parts of the match to be captured for|backward “ for” or “backward” (for|back)ward “ forward” or “backward”
Regular Expression Basics Delimiters pattern / modifiers / /i Makes match case-insensitive
Performing a Match ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Performing a Replacement ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Don’t Use Regular Expressions! Don’t Abuse Regular Expressions! Some people, when confronted with a problem, think “ I know, I'll use regular expressions.” Now they have two problems. — Jamie Zawinski
Testing for a Substring if  ( preg_match ( '/foo/' ,  $ var )) if  ( strpos ( $ var ,  'foo' )  !==   false ) if  ( preg_match ( '/foo/i' ,  $ var )) if  ( stripos ( $ var ,  'foo' )  !==   false )
Validating an Integer ,[object Object],[object Object],if  ( preg_match ( '/ ^  +$ /' ,  $ value )) { // $value is a positive integer } Regular Expression
Validating an Integer ,[object Object],[object Object],ctype (Character Type) if  ( ctype_digit ( $ value )) { // $value is a positive integer }
Validating an Integer ,[object Object],[object Object],[object Object],$ casted_value   =   intval ( $ value ); if  ( $ casted_value   >   0 ) { // $casted_value is a positive (non-zero) integer } Casting
HTML Parsing
Using Regular Expressions
Using Regular Expressions Postcodes /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}/ IP Addresses @^({1,2})/({1,2})/({4})$@
Constructing Patterns ,[object Object]
You don’t need to use  /…/ to denote a pattern! /…/ to denote a pattern! preg_match ( '/<b><s> .+ <  s> .+ <  b>/' ,  $ html ) preg_match ( '@<b><s> .+ </s> .+ </b>@' ,  $ html )
Greediness $ html   =   <<< HTML <span> some text </span><span> some more text! </span> HTML ; preg_match ( &quot;@<span>(.+)</span>@&quot; ,  $ html ,  $ matches ); echo   $ matches [ 0 ]; preg_match ( &quot;@<span>(.+?)</span>@&quot; ,  $ html ,  $ matches ); echo   $ matches [ 0 ];
You can make your pattern readable! preg_match ( '`^(+)://(?:(.+?):(.+?)@)?(.+?)(+)$`' ,  $ s ,  $ matches ) preg_match ( '` ^ (+)://  # Protocol (?: (.+?)  # Username :  # : (.+?)  # Password @  # @ )?  # Username/password are optional (.+?)  # Hostname (+)  # Top-level domain $ `x' , $ s ,  $ matches );
Extracting Captures preg_match ( '`^ (?P<protocol>+):// (?: (?P<user>.+?) : (?P<pass>.+?) @ )? (?P<host>.+?) (?P<tld>+) $`x' ,  $ s ,  $ matches ); Array(    [0] =>  http://foo:bar@baz.example.com     [protocol] => http    [1] => http    [user] => foo    [2] => foo    [pass] => bar    [3] => bar    [host] => baz.example    [4] => baz.example    [tld] => com    [5] => com) preg_match ( '`^ (?P<protocol>+):// (?: (?P<user>.+?) : (?P<pass>.+?) @ )? (?P<host>.+?) (?P<tld>+) $`x' ,  $ s ,  $ matches );
Variable Data if  ( preg_match ( &quot;!> $ value </(?:div|span)>!&quot; ,  $ text )) $ value   =   preg_quote ( $ value ,  '!' );
Performing Logic on Replacements preg_replace ( '/ + /e' ,  'strtoupper(&quot;&quot;)' ,  'foo bar baz' ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Testing Tools ,[object Object],[object Object],[object Object]
Any Questions?

More Related Content

What's hot

Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
Raj Rajandran
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raj Gupta
 

What's hot (20)

Regex cheatsheet
Regex cheatsheetRegex cheatsheet
Regex cheatsheet
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
 
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
 
Лекция 6. Хеш-таблицы
Лекция 6. Хеш-таблицыЛекция 6. Хеш-таблицы
Лекция 6. Хеш-таблицы
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Naive string matching
Naive string matchingNaive string matching
Naive string matching
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
04 brute force
04 brute force04 brute force
04 brute force
 
Regular expression
Regular expressionRegular expression
Regular expression
 
NLTK in 20 minutes
NLTK in 20 minutesNLTK in 20 minutes
NLTK in 20 minutes
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithm
 
Rabin karp string matching algorithm
Rabin karp string matching algorithmRabin karp string matching algorithm
Rabin karp string matching algorithm
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
RLHF_Lessons_learned.pdf
RLHF_Lessons_learned.pdfRLHF_Lessons_learned.pdf
RLHF_Lessons_learned.pdf
 
Extending Gremlin with Foundational Steps
Extending Gremlin with Foundational StepsExtending Gremlin with Foundational Steps
Extending Gremlin with Foundational Steps
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
 
Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in Python
 
arrays in c
arrays in carrays in c
arrays in c
 
Array and string
Array and stringArray and string
Array and string
 
TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
 

Viewers also liked (14)

S 1 (Sports Decorated Champions)
S 1 (Sports   Decorated Champions)S 1 (Sports   Decorated Champions)
S 1 (Sports Decorated Champions)
 
Excellent Testcase
Excellent TestcaseExcellent Testcase
Excellent Testcase
 
Virtual Marketing Pro VMP
Virtual Marketing Pro VMPVirtual Marketing Pro VMP
Virtual Marketing Pro VMP
 
Imageformats
ImageformatsImageformats
Imageformats
 
Macro
MacroMacro
Macro
 
El Trabajo Cooperativo Y Las Competencias LingüíSticas2
El Trabajo Cooperativo Y Las Competencias LingüíSticas2El Trabajo Cooperativo Y Las Competencias LingüíSticas2
El Trabajo Cooperativo Y Las Competencias LingüíSticas2
 
Problemas
ProblemasProblemas
Problemas
 
Corporateblogging
CorporatebloggingCorporateblogging
Corporateblogging
 
Presentacion
PresentacionPresentacion
Presentacion
 
Shakira
ShakiraShakira
Shakira
 
Presentación Pliegos 1 a 3
Presentación Pliegos 1 a 3Presentación Pliegos 1 a 3
Presentación Pliegos 1 a 3
 
She S A Lady
She S A LadyShe S A Lady
She S A Lady
 
Os Timed Original
Os Timed OriginalOs Timed Original
Os Timed Original
 
Office 2007 Service Pack 1 Changes All
Office 2007 Service Pack 1 Changes AllOffice 2007 Service Pack 1 Changes All
Office 2007 Service Pack 1 Changes All
 

Similar to Regex Basics

Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
mussawir20
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Sway Wang
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 

Similar to Regex Basics (20)

Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.net
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
 
Les08
Les08Les08
Les08
 
An Introduction to Regular expressions
An Introduction to Regular expressionsAn Introduction to Regular expressions
An Introduction to Regular expressions
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
 
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to 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
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 

More from Jeremy Coates

Search Lucene
Search LuceneSearch Lucene
Search Lucene
Jeremy Coates
 

More from Jeremy Coates (17)

Cyber Security and GDPR
Cyber Security and GDPRCyber Security and GDPR
Cyber Security and GDPR
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Why is PHP Awesome
Why is PHP AwesomeWhy is PHP Awesome
Why is PHP Awesome
 
Testing with Codeception
Testing with CodeceptionTesting with Codeception
Testing with Codeception
 
An introduction to Phing the PHP build system (PHPDay, May 2012)
An introduction to Phing the PHP build system (PHPDay, May 2012)An introduction to Phing the PHP build system (PHPDay, May 2012)
An introduction to Phing the PHP build system (PHPDay, May 2012)
 
An introduction to Phing the PHP build system
An introduction to Phing the PHP build systemAn introduction to Phing the PHP build system
An introduction to Phing the PHP build system
 
Insects in your mind
Insects in your mindInsects in your mind
Insects in your mind
 
Phing
PhingPhing
Phing
 
Hudson Continuous Integration for PHP
Hudson Continuous Integration for PHPHudson Continuous Integration for PHP
Hudson Continuous Integration for PHP
 
The Uncertainty Principle
The Uncertainty PrincipleThe Uncertainty Principle
The Uncertainty Principle
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
 
What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3
 
Kiss Phpnw08
Kiss Phpnw08Kiss Phpnw08
Kiss Phpnw08
 
Search Lucene
Search LuceneSearch Lucene
Search Lucene
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain Explained
 
Introduction to Version Control
Introduction to Version ControlIntroduction to Version Control
Introduction to Version Control
 
PHPNW Conference Update
PHPNW Conference UpdatePHPNW Conference Update
PHPNW Conference Update
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Regex Basics

  • 1.
  • 2.
  • 3. Regular Expression Basics Literals bus Matches a ‘ b ’, followed by a ‘ u ’, followed by an ‘ s ’
  • 4. Regular Expression Basics Anchors ^ Matches at the beginning of a line $ Matches at the end of a line
  • 5. Regular Expression Basics Character Classes [abc] Matches one of ‘ a ’, ‘ b ’ or ‘ c ’ [a-c] Same as above (character range) [^abc] Matches one character that is not listed . Matches any single character
  • 6. Regular Expression Basics Alternation a|b Matches one of ‘ a ’ or ‘ b ’ dog|cat Matches one of “dog” or “cat”
  • 7. Regular Expression Basics Quantifiers (repetition) {x,y} Matches minimum of x and a maximum of y occurrences; either can be omitted * Matches zero or more occurrences (any amount). Same as {0,} + Matches one or more occurrences. Same as {1,} ? Matches zero or one occurrences. Same as {0,1}
  • 8. Regular Expression Basics Grouping (…) Groups the contents of the parentheses. Affects alternation and quantifiers. Allows parts of the match to be captured for|backward “ for” or “backward” (for|back)ward “ forward” or “backward”
  • 9. Regular Expression Basics Delimiters pattern / modifiers / /i Makes match case-insensitive
  • 10.
  • 11.
  • 12.
  • 13. Testing for a Substring if ( preg_match ( '/foo/' , $ var )) if ( strpos ( $ var , 'foo' ) !== false ) if ( preg_match ( '/foo/i' , $ var )) if ( stripos ( $ var , 'foo' ) !== false )
  • 14.
  • 15.
  • 16.
  • 19. Using Regular Expressions Postcodes /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}/ IP Addresses @^({1,2})/({1,2})/({4})$@
  • 20.
  • 21. You don’t need to use /…/ to denote a pattern! /…/ to denote a pattern! preg_match ( '/<b><s> .+ < s> .+ < b>/' , $ html ) preg_match ( '@<b><s> .+ </s> .+ </b>@' , $ html )
  • 22. Greediness $ html = <<< HTML <span> some text </span><span> some more text! </span> HTML ; preg_match ( &quot;@<span>(.+)</span>@&quot; , $ html , $ matches ); echo $ matches [ 0 ]; preg_match ( &quot;@<span>(.+?)</span>@&quot; , $ html , $ matches ); echo $ matches [ 0 ];
  • 23. You can make your pattern readable! preg_match ( '`^(+)://(?:(.+?):(.+?)@)?(.+?)(+)$`' , $ s , $ matches ) preg_match ( '` ^ (+):// # Protocol (?: (.+?) # Username : # : (.+?) # Password @ # @ )? # Username/password are optional (.+?) # Hostname (+) # Top-level domain $ `x' , $ s , $ matches );
  • 24. Extracting Captures preg_match ( '`^ (?P<protocol>+):// (?: (?P<user>.+?) : (?P<pass>.+?) @ )? (?P<host>.+?) (?P<tld>+) $`x' , $ s , $ matches ); Array(    [0] => http://foo:bar@baz.example.com     [protocol] => http    [1] => http    [user] => foo    [2] => foo    [pass] => bar    [3] => bar    [host] => baz.example    [4] => baz.example    [tld] => com    [5] => com) preg_match ( '`^ (?P<protocol>+):// (?: (?P<user>.+?) : (?P<pass>.+?) @ )? (?P<host>.+?) (?P<tld>+) $`x' , $ s , $ matches );
  • 25. Variable Data if ( preg_match ( &quot;!> $ value </(?:div|span)>!&quot; , $ text )) $ value = preg_quote ( $ value , '!' );
  • 26.
  • 27.