SlideShare a Scribd company logo
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn (geoff@warmage.com)
What are Regular Expressions Regular expressions are a syntax to match text. They date back to mathematical notation made in the 1950s. Became embedded in unix systems through tools like ed and grep.
What are RE Perl in particular promoted the use of very complex regular expressions. They are now available in all popular programming languages. They allow much more complex matching than strpos()
Why use RE You can use RE to enforce rules on formats like phone numbers, email addresses or URLs. You can use them to find key data within logs, configuration files or webpages.
Why use RE They can quickly make replacements that may be complex like finding all email addresses in a page and making them address [AT] site [dot] com. You can make your code really hard to understand
Syntax basics The entire regular expression is a sequence of characters between two forward slashes (/) abc  -  most characters are normal character matches. This is looking for the exact character sequence a, b and then c . - a period will match any character (except a newline but that can change) [abc]  -  square brackets will match any of the characters inside. Here: a, b or c.
Syntax basics ? - marks the previous as optional. so a? means there  might  be an a (abc)* - parenthesis group patterns and the asterix marks zero or more of the previous character. So this would match an empty string or abcabcabcabc \.+ - the backslash is an all purpose escape character. the + marks one or more of the previous character. So this would match ......
More syntax tricks [0-4]  -  match any number from 0 to 4 [^0-4]  -  match anything not the number 0-4 \sword\s  -  match word where there is white space before and after \bword\b - \b marks a word boundary. This could be white space, new line or end of the string
More syntax tricks \d{3,12} - \d matches any digit ([0-9]) while the braces mark the min and max count of the previous character. In this case 3 to 12 digits [a-z]{8,} - must be at least 8 letters
Matching Text Simple check: preg_match(“/^[a-z0-9]+@([a-z0-9]+\.)*[a-z0-9]+$/i”, $email_address) > 0 Finding: preg_match(“/\bcolou?r:\s+([a-zA-Z]+)\b/”, $text, $matches); echo $matches[1]; Find all: preg_match_all(“/<([^>]+)>/”, $html, $tags); echo $tags[2][1];
Matching Lines This is more for looking through files but could be for any array of text. $new_lines = preg_grep(“/Jan[a-z]*[\s\/\-](20)?07/”, $old_lines); Or lines that do not match by adding a third parameter of PREG_GREP_INVERT rather than complicating your regular expression into something like /^[^\/]|(\/[^p])|(\/p[^r]) etc...
Replacing text preg_replace( “ /\b[^@]+(@)[a-zA-Z-_\d]+(\.)[a-zA-Z-_\d\.]+\b/”, array(“ [AT] “, “ [dot] “), $post);
Splitting text $date_parts = preg_split(“/[-\.,\/\\\s]+/”, $date_string);
Tips Comment what your regular expression is doing. Test your regular expression for speed. Some can cause a noticeable slowdown. There are plenty of simple uses like /Width: (\d+)/ Watch out for greedy expressions. Eg /(<(.+)>)/ will not pull out “b” and “/b” from “<b>test</b>” but instead will pull “b>test</b”. A easy way to change this behaviour is like this: /(<(.+?)>)/
References http://en.wikipedia.org/wiki/Regular_expressions http://php.net/manual/en/ref.pcre.php Thank you

More Related Content

PPT
PHP Regular Expressions
PPT
Php String And Regular Expressions
PPTX
Regular expressions and php
PPTX
Regular Expressions in PHP
ODP
Regular Expression
KEY
Regular Expressions 101
ODP
Regex Presentation
PPT
Regular Expressions grep and egrep
PHP Regular Expressions
Php String And Regular Expressions
Regular expressions and php
Regular Expressions in PHP
Regular Expression
Regular Expressions 101
Regex Presentation
Regular Expressions grep and egrep

What's hot (20)

PPT
Regular Expressions
PPT
Introduction to regular expressions
KEY
Andrei's Regex Clinic
PPTX
Regular Expression
PPTX
Regular Expressions 101 Introduction to Regular Expressions
PPTX
Finaal application on regular expression
PPT
Textpad and Regular Expressions
PPTX
Introduction to Regular Expressions
PPTX
Regular expressions
PPT
Introduction to Regular Expressions
PPTX
Regular expression examples
PPT
The Power of Regular Expression: use in notepad++
PPT
Regular expressions
PPTX
Regular expressions
PPTX
Regular expression
PPT
Regular Expression
ODP
Looking for Patterns
PDF
3.2 javascript regex
PPTX
Regular Expression (Regex) Fundamentals
PPT
Regular Expressions
Regular Expressions
Introduction to regular expressions
Andrei's Regex Clinic
Regular Expression
Regular Expressions 101 Introduction to Regular Expressions
Finaal application on regular expression
Textpad and Regular Expressions
Introduction to Regular Expressions
Regular expressions
Introduction to Regular Expressions
Regular expression examples
The Power of Regular Expression: use in notepad++
Regular expressions
Regular expressions
Regular expression
Regular Expression
Looking for Patterns
3.2 javascript regex
Regular Expression (Regex) Fundamentals
Regular Expressions
Ad

Similar to Regular Expressions 2007 (20)

PPT
regex.ppt
PPT
Regular Expressions in PHP, MySQL by programmerblog.net
PPT
Bioinformatica 06-10-2011-p2 introduction
PPTX
Bioinformatics p2-p3-perl-regexes v2014
PDF
Regex - Regular Expression Basics
PDF
Don't Fear the Regex - CapitalCamp/GovDays 2014
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
PPT
Perl Intro 5 Regex Matches And Substitutions
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
PDF
Don't Fear the Regex - Northeast PHP 2015
PDF
Don't Fear the Regex LSP15
PDF
Don't Fear the Regex WordCamp DC 2017
PPSX
Regular expressions in oracle
PDF
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
PDF
Basta mastering regex power
PPT
Regex Basics
PPTX
Regular Expression Crash Course
PPTX
Regular expressions
PDF
Regular expression for everyone
ODP
Regular Expressions and You
regex.ppt
Regular Expressions in PHP, MySQL by programmerblog.net
Bioinformatica 06-10-2011-p2 introduction
Bioinformatics p2-p3-perl-regexes v2014
Regex - Regular Expression Basics
Don't Fear the Regex - CapitalCamp/GovDays 2014
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
Perl Intro 5 Regex Matches And Substitutions
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex LSP15
Don't Fear the Regex WordCamp DC 2017
Regular expressions in oracle
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
Basta mastering regex power
Regex Basics
Regular Expression Crash Course
Regular expressions
Regular expression for everyone
Regular Expressions and You
Ad

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Cloud computing and distributed systems.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Electronic commerce courselecture one. Pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
Programs and apps: productivity, graphics, security and other tools
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
Spectroscopy.pptx food analysis technology
Network Security Unit 5.pdf for BCA BBA.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton

Regular Expressions 2007

  • 1. Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn (geoff@warmage.com)
  • 2. What are Regular Expressions Regular expressions are a syntax to match text. They date back to mathematical notation made in the 1950s. Became embedded in unix systems through tools like ed and grep.
  • 3. What are RE Perl in particular promoted the use of very complex regular expressions. They are now available in all popular programming languages. They allow much more complex matching than strpos()
  • 4. Why use RE You can use RE to enforce rules on formats like phone numbers, email addresses or URLs. You can use them to find key data within logs, configuration files or webpages.
  • 5. Why use RE They can quickly make replacements that may be complex like finding all email addresses in a page and making them address [AT] site [dot] com. You can make your code really hard to understand
  • 6. Syntax basics The entire regular expression is a sequence of characters between two forward slashes (/) abc - most characters are normal character matches. This is looking for the exact character sequence a, b and then c . - a period will match any character (except a newline but that can change) [abc] - square brackets will match any of the characters inside. Here: a, b or c.
  • 7. Syntax basics ? - marks the previous as optional. so a? means there might be an a (abc)* - parenthesis group patterns and the asterix marks zero or more of the previous character. So this would match an empty string or abcabcabcabc \.+ - the backslash is an all purpose escape character. the + marks one or more of the previous character. So this would match ......
  • 8. More syntax tricks [0-4] - match any number from 0 to 4 [^0-4] - match anything not the number 0-4 \sword\s - match word where there is white space before and after \bword\b - \b marks a word boundary. This could be white space, new line or end of the string
  • 9. More syntax tricks \d{3,12} - \d matches any digit ([0-9]) while the braces mark the min and max count of the previous character. In this case 3 to 12 digits [a-z]{8,} - must be at least 8 letters
  • 10. Matching Text Simple check: preg_match(“/^[a-z0-9]+@([a-z0-9]+\.)*[a-z0-9]+$/i”, $email_address) > 0 Finding: preg_match(“/\bcolou?r:\s+([a-zA-Z]+)\b/”, $text, $matches); echo $matches[1]; Find all: preg_match_all(“/<([^>]+)>/”, $html, $tags); echo $tags[2][1];
  • 11. Matching Lines This is more for looking through files but could be for any array of text. $new_lines = preg_grep(“/Jan[a-z]*[\s\/\-](20)?07/”, $old_lines); Or lines that do not match by adding a third parameter of PREG_GREP_INVERT rather than complicating your regular expression into something like /^[^\/]|(\/[^p])|(\/p[^r]) etc...
  • 12. Replacing text preg_replace( “ /\b[^@]+(@)[a-zA-Z-_\d]+(\.)[a-zA-Z-_\d\.]+\b/”, array(“ [AT] “, “ [dot] “), $post);
  • 13. Splitting text $date_parts = preg_split(“/[-\.,\/\\\s]+/”, $date_string);
  • 14. Tips Comment what your regular expression is doing. Test your regular expression for speed. Some can cause a noticeable slowdown. There are plenty of simple uses like /Width: (\d+)/ Watch out for greedy expressions. Eg /(<(.+)>)/ will not pull out “b” and “/b” from “<b>test</b>” but instead will pull “b>test</b”. A easy way to change this behaviour is like this: /(<(.+?)>)/