SlideShare a Scribd company logo
1 of 36
Download to read offline
Don't Fear the Regex
Sandy Smith - Lone Star PHP 2015
Regex Basics
Don't Fear the Regex
So what are Regular
Expressions?
“...a means for matching strings of text, such as
particular characters, words, or patterns of
characters.”
Source: http://en.wikipedia.org/wiki/
Regular_expression
3
Don't Fear the Regex
A Common Joke
Some people, when confronted with a problem, think:
'I know, I'll use regular expressions.'
Now they have two problems.
But really, it’s not that bad, and Regular
Expressions (Regex) are a powerful tool.
4
Don't Fear the Regex
So what are they good at?
Regex is good at one thing, and that is to match
patterns in strings. You might use this to:
• Scrape information off of a webpage
• Pull data out of text files
• Process/Validate data sent to you by a user
- Such as phone or credit card numbers
- Usernames or even addresses
• Evaluate URLs to process what code to execute
- Via a framework bootstrap router, or mod_rewrite
5
Don't Fear the Regex
So what do they not do?
You can only match/filter/replace patterns of
characters you expect to see, if you get
unexpected (or non-standard) input, you won’t be
able pull the patterns.
6
Don't Fear the Regex
Can you do this in PHP?
Yes! PHP contains a great regular expression library, the
Perl-Compatible Regular Expression (PCRE) engine.
• Perl was (is?) the gold-standard language for doing
text manipulation
• Lots of programmers knew its regex syntax.
• The first PHP regex engine was slow and used a
slightly different syntax
• PCRE was created to speed things up and let people
use their Perl skillz.
• Regexes are extremely useful in text editors!
7
Don’t Fear the Regex
Useful tools
Play along!
• http://www.phpliveregex.com
• https://www.regex101.com
• http://www.regexr.com
8
Pattern Matching
Don't Fear the Regex
Delimiters
All regex patterns are between delimiters.
By custom, because Perl did it, this is /
• e.g. '/regex-goes-here/'
However, PCRE allows anything other than letters,
numbers, whitespace, or a backslash () to be delimiters.
•'#regex-goes-here#'
Why have delimiters?
• All will become clear later.
10
Don't Fear the Regex
Straight Text Syntax
The most basic regexes match text like strpos() does:
• Take the string "PHP is my language of choice"
• /PHP/ will match, but /Ruby/ won't.
They start getting powerful when you add the ability to
only match text at the beginning or end of a line:
• Use ^ to match text at the beginning:
- /^PHP/ will match, but /^my/ won't.
• Use $ to match text at the end:
- /choice$/ will match, but /PHP$/ won't.
11
Don't Fear the Regex
Basic Pattern Matching
Regular expressions are often referred to as "pattern
matching," because that's what makes them powerful.
Use special characters to match patterns of text:
. matches any single character:
/P.P/ matches PHP or PIP
+ matches one or more of the previous character:
/PH+P/ matches PHP or PHHHP
* matches zero or more of the previous characters:
/PH*P/ matches PHP or PP or PHHHHP
12
Don't Fear the Regex
Basic Pattern Matching
? matches zero or one of the previous character:
/PH?P/ matches PHP or PP but not PHHP
{<min>,<max>} matches from min to max
occurrences of the previous character:
/PH{1,2}P/ matches PHP or PHHP but not PP or PHHHP
13
Don't Fear the Regex
Powerful Basic Patterns
You can use combinations of these patterns to find
lots of things. Here are the most common:
.? Find zero or one characters of any type:
/P.?P/ gets you PP, PHP, PIP, but not PHHP.
.+ Find one or more characters of any type:
/P.+P/ gets you PHP, PIP, PHPPHP, PIIIP, but not PP.
.* Find zero or more characters of any type:
/P.*P/ gets PP, PHP, PHPHP, PIIIP, but not PHX.
14
Don't Fear the Regex
Beware of Greed
.* and .+ are "greedy" by default, meaning they match as
much as they can while still fulfilling the pattern.
/P.+P/ will match not only "PHP" but "PHP PHP"
Greedy pattern don't care.
What if you want to only match "PHP", "PHHP", or "PIP",
but not "PHP PHP"?
? kills greed.
/P.*?P/ will match PHP, PP, or PIIIP but only the first
PHP in "PHP PHP"
Great for matching tags in HTML, e.g. /<.+?>/
15
Don't Fear the Regex
Matching literal symbols
If you need to match a character used as a symbol,
such as $, +, ., ^, or *, escape it by preceding it with
a backslash ().
/./ matches a literal period (.).
/^$/ matches a dollar sign at the beginning of a
string.
16
Don't Fear the Regex
Calling this in PHP
To match regular expressions in PHP, use
preg_match().
It returns 1 if a pattern is matched and 0 if not. It
returns false if you blew your regex syntax.
Simplest Example:
17
$subject = "PHP regex gives PHP PEP!";
$found = preg_match("/P.P/", $subject);
echo $found; // returns 1
Character Classes and
Subpatterns
Don't Fear the Regex
Character Classes
Matching any character can be powerful, but lots of
times you'll want to only match specific characters.
Enter character classes.
• Character classes are enclosed by [ and ] (square
brackets)
• Character classes can be individual characters
• They can also be ranges
• They can be any combination of the above
• No "glue" character: any character is a valid pattern
19
Don't Fear the Regex
Character Class Examples
Single character:
[aqT,] matches a, q,T (note the case), or a comma (,)
Range:
[a-c] matches either a, b, or c (but not A or d or ...)
[4-6] matches 4, 5, or 6
Combination
[a-c4z6-8] matches a, b, c, 4, z, 6, 7, or 8
20
Don't Fear the Regex
Negative classes
Even more powerful is the ability to match anything
except characters in a character class.
• Negative classes are denoted by ^ at the beginning
of the class
• [^a] matches any character except a
• [^a-c] matches anything except a, b, or c
• [^,0-9] matches anything except commas or digits
21
Don't Fear the Regex
Using Character Classes
Just using the elements you've learned so far, you can
write the majority of patterns commonly used in
regular expressions.
/<[^>]+?/>/ matches all the text inside an HTML tag
/^[0-9]+/ matches the same digits PHP will when
casting a string to an integer.
/^[a-zA-Z0-9]+$/ matches a username that must be
only alphanumeric characters
/^$[a-zA-Z_][a-zA-Z0-9_]*$/ matches a valid
variable name in PHP.
22
Don't Fear the Regex
Subpatterns
What if you want to look for a pattern within a pattern? Or
a specific sequence of characters? It's pattern inception with
subpatterns.
• Subpatterns are enclosed by ( and ) (parentheses)
• They can contain a string of characters to match as a
group, such as (cat)
• Combined with other symbols, this means you can look
for catcatcat with (cat)+
• You can look for alternate strings, such as (cat|dog)
matching cat or dog
• They can also contain character classes and expressions
23
Don't Fear the Regex
Revisiting preg_match()
What if you want to extract the text that's been matched?
• preg_match() has an optional third argument for an array
that it will fill with the matched results.
• Why an array? Because it assumes you'll be using
subpatterns.
• The first element of the array is the text matched by your
entire pattern.
• The second element is the text matched by the first
subpattern (from left to right), the second with the
second, and so on.
• The array is passed by reference for extra confusion.
24
Don't Fear the Regex
Matching with Subpatterns
<?php
$variable = '$variable';
$pattern = ‘/^$([a-zA-Z_][a-zA-Z_0-9]*)$/';
$matches = array();
$result = preg_match($pattern, $variable, $matches);
var_dump($matches); // passed by reference
/*
array(2) {
[0]=>
string(9) "$variable"
[1]=>
string(8) "variable"
}
*/
25
Don't Fear the Regex
Alternatives
Subpatterns can do more than simply group patterns
for back references.They can also let you identify
strings of alternatives that you can match, using the
pipe character (|) to separate them.
For example, /(cat|dog)/ will match cat or dog.
When combined with other patterns, it becomes
powerful:
/^((http|https|ftp|gopher|file)://)?([^.]+?)/
would let you match the first domain or subdomain
of a URL.
26
Don't Fear the Regex
Escape Sequences
Now that we've made you write [0-9] a whole
bunch of times, let's show you a shortcut for that
plus a bunch of others. (Ain't we a pill?)
• d gets you any digit. D gets you anything that
isn't a digit.
• s gets you any whitespace character. Careful,
this usually* includes newlines (n) and carriage
returns (r). S gets you anything not
whitespace.
27
Don't Fear the Regex
Escape Sequences (cont'd)
You've already seen how to escape special characters used
in regular expressions as well as replacements for character
classes.What about specific whitespace characters?
• t is a tab character.
• n is the Unix newline (line feed); also the default line
ending in PHP.
• r is the carriage return. Formerly used on Macs. rn is
Windows's end of line statement; R gets you all three.
• h gets you any non-line ending whitespace character
(horizontal whitespace).
28
Don't Fear the Regex
Special Escape Sequences
There are some oddities that are holdovers from the
way Perl thinks about regular expressions.
• w gets you a "word" character, which means

[a-zA-Z0-9_] (just like a variable!), but is locale-aware
(captures accents in other languages). W is everything
else. I'm sure Larry Wall has a long-winded
explanation. Note it doesn't include a hyphen (-) or
apostrophe (').
• b is even weirder. It's a "word boundary," so not a
character, per se, but marking the transition between
whitespace and "word" characters as defined by w.
29
Don't Fear the Regex
Back References
Rather than repeat complicated subpatterns, you can
use a back reference.
Each back reference is denoted by a backslash and the
ordinal number of the subpattern. (e.g., 1, 2, 3, etc.)
As in preg_match(), subpatterns count left
parentheses from left to right.
• In /(outersub(innersub))12/, 1 matches
outersub(innersub), and 2 matches innersub.
• Similarly, in /(sub1)(sub2)12/, 1 matches
sub1, and 2 matches sub2.
30
Don't Fear the Regex
Back Reference Example
The easiest real-world example of a back reference
is matching closing to opening quotes, whether they
are single or double.
31
$subject = 'Get me "stuff in quotes."';
$pattern = '/(['"])(.*?)1/';
$matches = array();
$result = preg_match($pattern, $subject, $matches);
var_dump($matches);
/*
array(3) {
[0]=>
string(18) ""stuff in quotes.""
[1]=>
string(1) """
[2]=>
string(16) "stuff in quotes."
}
*/
Don't Fear the Regex
Replacing
Matching is great, but what about manipulating
strings?
Enter preg_replace().
Instead of having a results array passed by
reference, preg_replace returns the altered
string.
It can also work with arrays. If you supply an array,
it performs the replace on each element of the
array and returns an altered array.
32
Don't Fear the Regex
preg_replace()
<?php
$pattern = '/(['"]).*?1/';
$subject = 'Look at the "stuff in quotes!"';
$replacement = '$1quoted stuff!$1';
$result = preg_replace($pattern, $replacement, $subject);
echo $result; // Look at the "quoted stuff!"
$pattern = array('/overweight/', '/brown/', '/fox/');
$subject = array('overweight programmer', 'quick brown fox', 'spry red fox');
$replacement = array('#thintheherd', 'black', 'bear');
$result = preg_replace($pattern, $replacement, $subject);
var_dump($result);
/* array(3) {
[0]=>
string(21) “#thintheherd programmer"
[1]=>
string(15) "quick black bear"
[2]=>
string(13) "spry red bear"
} */
33
Don't Fear the Regex
Case-insensitive modifier
Remember when we said we’d explain why regular
expressions use delimiters? By now, some of you
may have asked about case-sensitivity, too, and we
said we’d get to it later. Now is the time for both.
Regular expressions can have options that modify
the behavior of the whole expression.These are
placed after the expression, outside the delimiters.
Simplest example: i means the expression is case-
insensitive. /asdf/i matches ASDF, aSDf, and asdf.
34
Don't Fear the Regex
When not to use Regex?
One more important topic. Regular expressions are
powerful, but when abused, they can lead to harder-to-
maintain code, security vulnerabilities, and other bad things.
In particular, don’t reinvent the wheel. PHP already has
great, tested libraries for filtering and validating input input
(filter_var) and parsing URLs (parse_url). Use them.
The rules for valid email addresses are surprisingly vague,
so best practice is to simply look for an @ or use
filter_var’s FILTER_VALIDATE_EMAIL and try to send
an email to the supplied address with a confirmation link.
35
Don't Fear the Regex
Thank you!
There’s much more to learn!
phparch.com/training
Follow us on Twitter:
@phparch
@SandyS1 - Me
Feedback is always welcome:
https://joind.in/13575
training@phparch.com
36

More Related Content

What's hot

Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expressionGagan019
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007Geoffrey Dunn
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式ji guang
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RHellen Gakuruh
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular ExpressionsOCSI
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in PracticeWeek-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in PracticeBertram Ludäscher
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and YouJames Armes
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linuxTeja Bheemanapally
 
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Theory
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in TheoryWeek-2: Theory & Practice of Data Cleaning: Regular Expressions in Theory
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in TheoryBertram Ludäscher
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for PatternsKeith Wright
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101Raj Rajandran
 

What's hot (20)

Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expression
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular Expressions
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
PHP Regular Expressions
PHP Regular ExpressionsPHP Regular Expressions
PHP Regular Expressions
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in PracticeWeek-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
 
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Theory
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in TheoryWeek-2: Theory & Practice of Data Cleaning: Regular Expressions in Theory
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Theory
 
2.regular expressions
2.regular expressions2.regular expressions
2.regular expressions
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Quick start reg ex
Quick start reg exQuick start reg ex
Quick start reg ex
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
 

Viewers also liked

Regular expressions
Regular expressionsRegular expressions
Regular expressionsJames Gray
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsNicole Ryan
 
Lessons from a Dying CMS
Lessons from a Dying CMSLessons from a Dying CMS
Lessons from a Dying CMSSandy Smith
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsdavidfstr
 
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Sandy Smith
 
Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"Daniel_Rhodes
 
Multibyte string handling in PHP
Multibyte string handling in PHPMultibyte string handling in PHP
Multibyte string handling in PHPDaniel_Rhodes
 
TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)Herri Setiawan
 
GAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analyticsGAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analyticsAnkita Kishore
 
Unicode Regular Expressions
Unicode Regular ExpressionsUnicode Regular Expressions
Unicode Regular ExpressionsNova Patch
 
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Sandy Smith
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?daoswald
 
Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015Sandy Smith
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQLNicole Ryan
 
How to report a bug
How to report a bugHow to report a bug
How to report a bugSandy Smith
 
EDUPUB 2013: Schema.org LRMI and A11Y for Discovery
EDUPUB 2013: Schema.org LRMI and A11Y for DiscoveryEDUPUB 2013: Schema.org LRMI and A11Y for Discovery
EDUPUB 2013: Schema.org LRMI and A11Y for DiscoveryGerardo Capiel
 
Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...Volha Bryl
 

Viewers also liked (20)

Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Lessons from a Dying CMS
Lessons from a Dying CMSLessons from a Dying CMS
Lessons from a Dying CMS
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)
 
Grokking regex
Grokking regexGrokking regex
Grokking regex
 
Dom
DomDom
Dom
 
Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"Hyperlocalisation or "localising everything"
Hyperlocalisation or "localising everything"
 
Multibyte string handling in PHP
Multibyte string handling in PHPMultibyte string handling in PHP
Multibyte string handling in PHP
 
TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)TDA Center Depok update 2014 (Concept)
TDA Center Depok update 2014 (Concept)
 
GAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analyticsGAIQ - Regular expressions-google-analytics
GAIQ - Regular expressions-google-analytics
 
Unicode Regular Expressions
Unicode Regular ExpressionsUnicode Regular Expressions
Unicode Regular Expressions
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
 
Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015Architecting with Queues - Northeast PHP 2015
Architecting with Queues - Northeast PHP 2015
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQL
 
How to report a bug
How to report a bugHow to report a bug
How to report a bug
 
EDUPUB 2013: Schema.org LRMI and A11Y for Discovery
EDUPUB 2013: Schema.org LRMI and A11Y for DiscoveryEDUPUB 2013: Schema.org LRMI and A11Y for Discovery
EDUPUB 2013: Schema.org LRMI and A11Y for Discovery
 
Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...Learning Regular Expressions for the Extraction of Product Attributes from E-...
Learning Regular Expressions for the Extraction of Product Attributes from E-...
 

Similar to Don't Fear the Regex LSP15

/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/ibrettflorio
 
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
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionskeeyre
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Ahmed El-Arabawy
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expressionazzamhadeel89
 
RegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing ExamplesRegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing Exampleszeteo12
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfBryan Alejos
 
CiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG
 
Introduction to Boost regex
Introduction to Boost regexIntroduction to Boost regex
Introduction to Boost regexYongqiang Li
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameAntony Stubbs
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitRon Reiter
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 

Similar to Don't Fear the Regex LSP15 (20)

/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
 
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 lecture
Regex lectureRegex lecture
Regex lecture
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: 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
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Regular expression for everyone
Regular expression for everyoneRegular expression for everyone
Regular expression for everyone
 
RegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing ExamplesRegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing Examples
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
CiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex Presentation
 
Introduction to Boost regex
Introduction to Boost regexIntroduction to Boost regex
Introduction to Boost regex
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and Git
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 

Recently uploaded

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Recently uploaded (20)

Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

Don't Fear the Regex LSP15

  • 1. Don't Fear the Regex Sandy Smith - Lone Star PHP 2015
  • 3. Don't Fear the Regex So what are Regular Expressions? “...a means for matching strings of text, such as particular characters, words, or patterns of characters.” Source: http://en.wikipedia.org/wiki/ Regular_expression 3
  • 4. Don't Fear the Regex A Common Joke Some people, when confronted with a problem, think: 'I know, I'll use regular expressions.' Now they have two problems. But really, it’s not that bad, and Regular Expressions (Regex) are a powerful tool. 4
  • 5. Don't Fear the Regex So what are they good at? Regex is good at one thing, and that is to match patterns in strings. You might use this to: • Scrape information off of a webpage • Pull data out of text files • Process/Validate data sent to you by a user - Such as phone or credit card numbers - Usernames or even addresses • Evaluate URLs to process what code to execute - Via a framework bootstrap router, or mod_rewrite 5
  • 6. Don't Fear the Regex So what do they not do? You can only match/filter/replace patterns of characters you expect to see, if you get unexpected (or non-standard) input, you won’t be able pull the patterns. 6
  • 7. Don't Fear the Regex Can you do this in PHP? Yes! PHP contains a great regular expression library, the Perl-Compatible Regular Expression (PCRE) engine. • Perl was (is?) the gold-standard language for doing text manipulation • Lots of programmers knew its regex syntax. • The first PHP regex engine was slow and used a slightly different syntax • PCRE was created to speed things up and let people use their Perl skillz. • Regexes are extremely useful in text editors! 7
  • 8. Don’t Fear the Regex Useful tools Play along! • http://www.phpliveregex.com • https://www.regex101.com • http://www.regexr.com 8
  • 10. Don't Fear the Regex Delimiters All regex patterns are between delimiters. By custom, because Perl did it, this is / • e.g. '/regex-goes-here/' However, PCRE allows anything other than letters, numbers, whitespace, or a backslash () to be delimiters. •'#regex-goes-here#' Why have delimiters? • All will become clear later. 10
  • 11. Don't Fear the Regex Straight Text Syntax The most basic regexes match text like strpos() does: • Take the string "PHP is my language of choice" • /PHP/ will match, but /Ruby/ won't. They start getting powerful when you add the ability to only match text at the beginning or end of a line: • Use ^ to match text at the beginning: - /^PHP/ will match, but /^my/ won't. • Use $ to match text at the end: - /choice$/ will match, but /PHP$/ won't. 11
  • 12. Don't Fear the Regex Basic Pattern Matching Regular expressions are often referred to as "pattern matching," because that's what makes them powerful. Use special characters to match patterns of text: . matches any single character: /P.P/ matches PHP or PIP + matches one or more of the previous character: /PH+P/ matches PHP or PHHHP * matches zero or more of the previous characters: /PH*P/ matches PHP or PP or PHHHHP 12
  • 13. Don't Fear the Regex Basic Pattern Matching ? matches zero or one of the previous character: /PH?P/ matches PHP or PP but not PHHP {<min>,<max>} matches from min to max occurrences of the previous character: /PH{1,2}P/ matches PHP or PHHP but not PP or PHHHP 13
  • 14. Don't Fear the Regex Powerful Basic Patterns You can use combinations of these patterns to find lots of things. Here are the most common: .? Find zero or one characters of any type: /P.?P/ gets you PP, PHP, PIP, but not PHHP. .+ Find one or more characters of any type: /P.+P/ gets you PHP, PIP, PHPPHP, PIIIP, but not PP. .* Find zero or more characters of any type: /P.*P/ gets PP, PHP, PHPHP, PIIIP, but not PHX. 14
  • 15. Don't Fear the Regex Beware of Greed .* and .+ are "greedy" by default, meaning they match as much as they can while still fulfilling the pattern. /P.+P/ will match not only "PHP" but "PHP PHP" Greedy pattern don't care. What if you want to only match "PHP", "PHHP", or "PIP", but not "PHP PHP"? ? kills greed. /P.*?P/ will match PHP, PP, or PIIIP but only the first PHP in "PHP PHP" Great for matching tags in HTML, e.g. /<.+?>/ 15
  • 16. Don't Fear the Regex Matching literal symbols If you need to match a character used as a symbol, such as $, +, ., ^, or *, escape it by preceding it with a backslash (). /./ matches a literal period (.). /^$/ matches a dollar sign at the beginning of a string. 16
  • 17. Don't Fear the Regex Calling this in PHP To match regular expressions in PHP, use preg_match(). It returns 1 if a pattern is matched and 0 if not. It returns false if you blew your regex syntax. Simplest Example: 17 $subject = "PHP regex gives PHP PEP!"; $found = preg_match("/P.P/", $subject); echo $found; // returns 1
  • 19. Don't Fear the Regex Character Classes Matching any character can be powerful, but lots of times you'll want to only match specific characters. Enter character classes. • Character classes are enclosed by [ and ] (square brackets) • Character classes can be individual characters • They can also be ranges • They can be any combination of the above • No "glue" character: any character is a valid pattern 19
  • 20. Don't Fear the Regex Character Class Examples Single character: [aqT,] matches a, q,T (note the case), or a comma (,) Range: [a-c] matches either a, b, or c (but not A or d or ...) [4-6] matches 4, 5, or 6 Combination [a-c4z6-8] matches a, b, c, 4, z, 6, 7, or 8 20
  • 21. Don't Fear the Regex Negative classes Even more powerful is the ability to match anything except characters in a character class. • Negative classes are denoted by ^ at the beginning of the class • [^a] matches any character except a • [^a-c] matches anything except a, b, or c • [^,0-9] matches anything except commas or digits 21
  • 22. Don't Fear the Regex Using Character Classes Just using the elements you've learned so far, you can write the majority of patterns commonly used in regular expressions. /<[^>]+?/>/ matches all the text inside an HTML tag /^[0-9]+/ matches the same digits PHP will when casting a string to an integer. /^[a-zA-Z0-9]+$/ matches a username that must be only alphanumeric characters /^$[a-zA-Z_][a-zA-Z0-9_]*$/ matches a valid variable name in PHP. 22
  • 23. Don't Fear the Regex Subpatterns What if you want to look for a pattern within a pattern? Or a specific sequence of characters? It's pattern inception with subpatterns. • Subpatterns are enclosed by ( and ) (parentheses) • They can contain a string of characters to match as a group, such as (cat) • Combined with other symbols, this means you can look for catcatcat with (cat)+ • You can look for alternate strings, such as (cat|dog) matching cat or dog • They can also contain character classes and expressions 23
  • 24. Don't Fear the Regex Revisiting preg_match() What if you want to extract the text that's been matched? • preg_match() has an optional third argument for an array that it will fill with the matched results. • Why an array? Because it assumes you'll be using subpatterns. • The first element of the array is the text matched by your entire pattern. • The second element is the text matched by the first subpattern (from left to right), the second with the second, and so on. • The array is passed by reference for extra confusion. 24
  • 25. Don't Fear the Regex Matching with Subpatterns <?php $variable = '$variable'; $pattern = ‘/^$([a-zA-Z_][a-zA-Z_0-9]*)$/'; $matches = array(); $result = preg_match($pattern, $variable, $matches); var_dump($matches); // passed by reference /* array(2) { [0]=> string(9) "$variable" [1]=> string(8) "variable" } */ 25
  • 26. Don't Fear the Regex Alternatives Subpatterns can do more than simply group patterns for back references.They can also let you identify strings of alternatives that you can match, using the pipe character (|) to separate them. For example, /(cat|dog)/ will match cat or dog. When combined with other patterns, it becomes powerful: /^((http|https|ftp|gopher|file)://)?([^.]+?)/ would let you match the first domain or subdomain of a URL. 26
  • 27. Don't Fear the Regex Escape Sequences Now that we've made you write [0-9] a whole bunch of times, let's show you a shortcut for that plus a bunch of others. (Ain't we a pill?) • d gets you any digit. D gets you anything that isn't a digit. • s gets you any whitespace character. Careful, this usually* includes newlines (n) and carriage returns (r). S gets you anything not whitespace. 27
  • 28. Don't Fear the Regex Escape Sequences (cont'd) You've already seen how to escape special characters used in regular expressions as well as replacements for character classes.What about specific whitespace characters? • t is a tab character. • n is the Unix newline (line feed); also the default line ending in PHP. • r is the carriage return. Formerly used on Macs. rn is Windows's end of line statement; R gets you all three. • h gets you any non-line ending whitespace character (horizontal whitespace). 28
  • 29. Don't Fear the Regex Special Escape Sequences There are some oddities that are holdovers from the way Perl thinks about regular expressions. • w gets you a "word" character, which means
 [a-zA-Z0-9_] (just like a variable!), but is locale-aware (captures accents in other languages). W is everything else. I'm sure Larry Wall has a long-winded explanation. Note it doesn't include a hyphen (-) or apostrophe ('). • b is even weirder. It's a "word boundary," so not a character, per se, but marking the transition between whitespace and "word" characters as defined by w. 29
  • 30. Don't Fear the Regex Back References Rather than repeat complicated subpatterns, you can use a back reference. Each back reference is denoted by a backslash and the ordinal number of the subpattern. (e.g., 1, 2, 3, etc.) As in preg_match(), subpatterns count left parentheses from left to right. • In /(outersub(innersub))12/, 1 matches outersub(innersub), and 2 matches innersub. • Similarly, in /(sub1)(sub2)12/, 1 matches sub1, and 2 matches sub2. 30
  • 31. Don't Fear the Regex Back Reference Example The easiest real-world example of a back reference is matching closing to opening quotes, whether they are single or double. 31 $subject = 'Get me "stuff in quotes."'; $pattern = '/(['"])(.*?)1/'; $matches = array(); $result = preg_match($pattern, $subject, $matches); var_dump($matches); /* array(3) { [0]=> string(18) ""stuff in quotes."" [1]=> string(1) """ [2]=> string(16) "stuff in quotes." } */
  • 32. Don't Fear the Regex Replacing Matching is great, but what about manipulating strings? Enter preg_replace(). Instead of having a results array passed by reference, preg_replace returns the altered string. It can also work with arrays. If you supply an array, it performs the replace on each element of the array and returns an altered array. 32
  • 33. Don't Fear the Regex preg_replace() <?php $pattern = '/(['"]).*?1/'; $subject = 'Look at the "stuff in quotes!"'; $replacement = '$1quoted stuff!$1'; $result = preg_replace($pattern, $replacement, $subject); echo $result; // Look at the "quoted stuff!" $pattern = array('/overweight/', '/brown/', '/fox/'); $subject = array('overweight programmer', 'quick brown fox', 'spry red fox'); $replacement = array('#thintheherd', 'black', 'bear'); $result = preg_replace($pattern, $replacement, $subject); var_dump($result); /* array(3) { [0]=> string(21) “#thintheherd programmer" [1]=> string(15) "quick black bear" [2]=> string(13) "spry red bear" } */ 33
  • 34. Don't Fear the Regex Case-insensitive modifier Remember when we said we’d explain why regular expressions use delimiters? By now, some of you may have asked about case-sensitivity, too, and we said we’d get to it later. Now is the time for both. Regular expressions can have options that modify the behavior of the whole expression.These are placed after the expression, outside the delimiters. Simplest example: i means the expression is case- insensitive. /asdf/i matches ASDF, aSDf, and asdf. 34
  • 35. Don't Fear the Regex When not to use Regex? One more important topic. Regular expressions are powerful, but when abused, they can lead to harder-to- maintain code, security vulnerabilities, and other bad things. In particular, don’t reinvent the wheel. PHP already has great, tested libraries for filtering and validating input input (filter_var) and parsing URLs (parse_url). Use them. The rules for valid email addresses are surprisingly vague, so best practice is to simply look for an @ or use filter_var’s FILTER_VALIDATE_EMAIL and try to send an email to the supplied address with a confirmation link. 35
  • 36. Don't Fear the Regex Thank you! There’s much more to learn! phparch.com/training Follow us on Twitter: @phparch @SandyS1 - Me Feedback is always welcome: https://joind.in/13575 training@phparch.com 36