SlideShare a Scribd company logo
Regular Expressions and You

An introduction to regular expressions.




James I. Armes
Web Developer, AllPlayers.com
@jamesiarmes
Email Validation Examples




 ^[w.%+-]+@[w.-]+.[A-Za-z]{2,4}$
Email Validation Examples
(?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:
[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 
000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[
["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*|(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:
(?:rn)?[ t])*)*<(?:(?:rn)?[ t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[
t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+
(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[
["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[ t])*)?(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:
(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:
(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:
[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*>(?:(?:rn)?[ t])*)|(?:[^()<>@,;:".[] 000-031]+(?:(?:
(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)*:(?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?
=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:
[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?
[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*|(?:[^()<>@,;:".[] 000-031]+
(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)*<(?:(?:rn)?[ t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])
+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^
[]r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:
(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[ t])*)?(?:[^()<>@,;:".[] 
000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:
(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[
["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:
(?:rn)?[ t])*))*>(?:(?:rn)?[ t])*)(?:,s*(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)
(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:
[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:
(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*|(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:
(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)*<(?:(?:rn)?[ t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.
(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 
000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[
["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[ t])*)?(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:
(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:
(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:
[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*>(?:(?:rn)?[ t])*))*)?;s*)
Types of Regular Expressions

●   Simple Regular Expressions
●   POSIX Basic Regular Expressions
●   POSIX Extended Regular Expressions
●   Perl Regular Expressions
Simple Regular Expressions
●   Traditional regular expressions.
●   Not a standard.
●   Support by some applications for backwards
    compatibility.
●   Deprecated.
POSIX Basic Regular
             Expressions
●   Created to provide a common standard for Unix
    tools.
●   Designed to be backwards compatible with
    traditional regular expressions.
●   Adopted as the default syntax of many Unix
    tools.
●   Some metacharacters require escaping.
POSIX Extended Regular
             Expressions
●   Adds some new metacharacters.
●   Metacharacters do not require escaping.
●   Dropped support for back references (n).
●   Many Unix tools provide support with a
    command line argument (usually -E).
Perl Regular Expressions

●   Adds lazy quantification, named capture groups
    and recursive patterns.
●   Adopted by many programming languages due
    to its power.
●   Requires non-alphanumeric delimiters around
    expression.
●   Other languages only implement a subset, so
    implementations vary.
Syntax
Basic Metacharacters

.   Match any single character.

^   Matches beginning of a string.

$   Matches end of a string.

|   Matches the expression before or after (think ||).
Character Classes

[]      Match any characters within the group.
[^ ]    Match any characters NOT within the group.
[n-m]   Match a range of characters.




Examples:
[A-Za-z0-9]
[^G-Zg-z _]
Shorthand Character Classes

s           Any whitespace character such as space, tab and newlines.
             Same as [nrt ]
w           Any word character.
             Same as [A-Za-z0-9_]
d           Any digit character.
             Same as [0-9]
S, W, D   Negated version of the above. Can be used inside character
             classes but could be confusing.
Quantifiers

*       Match the preceding expression 0 or more times.
+       Match the preceding expression 1 or more times.
?       Match the preceding expression 0 or 1 time.
{m,n}   Match the preceding expression at least m times but no more than n times.
{m,}    Match the preceding expression at least m times with no maximum.
{,n}    Match the preceding expression no more than n times with no minimum.
{n}     Match the preceding expression exactly n times.
Lazy Quantifiers

Standard Quantifiers are greedy.
Example:
Many programming courses start with a "Hello World" example.
That would be "Hallo Welt" in German.
"Hello .*"
Many programming courses start with a "Hello World" example.
That would be "Hallo Welt" in German.
Lazy Quantifiers

Use ? to make a quantifier lazy.
Example:
Many programming courses start with a "Hello World" example.
That would be "Hallo Welt" in German.
"Hello .*?"
Many programming courses start with a "Hello World" example.
That would be "Hallo Welt" in German.
Grouping

()      Group the expression and capture the text.
(?: )   Group the expression but DO NOT capture the text.
Backreferences

1 through 9 reference previously captured text.
Example:
Many programming courses start with a "Hello World"
example. 'Hello World' examples are extremely simple,
especially when they just output "Hello World'.
('|")Hello World(1)
Many programming courses start with a "Hello World"
example. 'Hello World' examples are extremely simple,
especially when they just output "Hello World'.
Word Boundaries

b matches the position between a word character
(w) and a non-word character (W).
Example:
Hello World
ob
Hello| World
Word Boundaries

B matches the position between two word
characters (ww).
Example:
Hello World
oB
Hello Wo|rld
Lookaheads

(?= ) matches the position directly before the
expression is matched.
Example:
Hello World sounds better than "Hello Earth".
Hello(?= World)
Hello World sounds better than "Hello Earth".
Lookbehinds

(?<= ) matches the position directly after the
expression is matched.
Example:
Hello World sounds better than "Hello Earth".
(?<=")Hello
Hello World sounds better than "Hello Earth".
Lookaheads

(?! ) matches the position directly before the
expression is NOT matched.
Example:
Hello World sounds better than "Hello Earth".
Hello(?! World)
Hello World sounds better than "Hello Earth".
Lookbehinds

(?<! ) matches the position directly after the
expression is NOT matched.
Example:
Hello World sounds better than "Hello Earth".
(?<!")Hello
Hello World sounds better than "Hello Earth".
Conditionals

(?(condition)then|else)
●   condition must be a lookahead or a lookbehind.
●   If condition is matched, then must match for the
    expression to pass.
●   If condition is not matched, else must match for
    the expression to pass.
Conditionals

Example:
Hello World sounds better than "Hello Earth".
Hello (?(?<=World)World|Earth)
Hello World sounds better than "Hello Earth".
Hello (?(?<=People)People|Earth)
Hello World sounds better than "Hello Earth".
Modifiers

i   Case insensitive matching.
s   . matches newline characters.
m   ^ and $ match after and before newlines (respectively).
x   Whitespace within the expression is ignored unless escaped.
g   Match globally.
Modifiers

●   (?a) to turn modifiers on.
●(?-a) to turn modifiers off.
Examples:
(?i)WORLD(?-i)
(?i-s)WORLD.(?s-i)
(?i:WORLD)
Language
Implementations
JavaScript

●   RegExp object.
        –   var expression = new RegExp('World', 'g');
        –   var expression = /World/g;
●   String.match()
●   String.replace()
●   String.split()
Perl

●   if ($string =~ /regex/)
●   $string =~ s/regex/replacement/
●   Regexp::Common
        –   http://search.cpan.org/dist/Regexp-Common/
        –   Provides common expressions.
        –   Examples:
                ●   IP Address
                ●   Credit Card Number
                ●   Profanity
PHP

●   ereg vs. preg
       –   preg uses Perl syntax.
       –   ereg uses POSIX Extended syntax.
       –   preg is much faster.
       –   ereg has been deprecated as of PHP 5.3.
PHP

●   preg_match()
●   preg_match_all()
●   preg_replace()
●   preg_split()
●   preg_quote()
●   http://www.php.net/manual/en/book.pcre.php
●   http://php.net/manual/reference.pcre.pattern.modifiers.php
Tools and Resources

●   txt2regex - http://aurelio.net/txt2regex/
●   Reggy (mac) - http://reggyapp.com/
●   Patterns (mac) - http://krillapps.com/patterns/
●   Web based - http://regex.larsolavtorvik.com/
●   Regular-Expressions.info (reference) -
    http://www.regular-expressions.info/
Thanks!




http://xkcd.com/208/

More Related Content

PPT
Regular Expressions
KEY
Andrei's Regex Clinic
PPTX
Regular expressions
PPTX
Regular Expressions 101 Introduction to Regular Expressions
PPTX
Introduction to Regular Expressions
PPTX
Regular expression
PPTX
Regular Expression
PPT
Introduction to regular expressions
Regular Expressions
Andrei's Regex Clinic
Regular expressions
Regular Expressions 101 Introduction to Regular Expressions
Introduction to Regular Expressions
Regular expression
Regular Expression
Introduction to regular expressions

What's hot (20)

PPTX
Finaal application on regular expression
PPT
Textpad and Regular Expressions
PPTX
Regular expressions
PPT
2.regular expressions
PDF
Basta mastering regex power
PPT
16 Java Regex
PPTX
Regular expressions
PPTX
Regular Expression (Regex) Fundamentals
PPT
Regular Expressions grep and egrep
PPTX
Python advanced 2. regular expression in python
ODP
Regex Presentation
ODP
Looking for Patterns
PPT
Regular expressions
ODP
Regular Expression
PPT
Regex Basics
KEY
Regular Expressions 101
PPT
Introduction to Regular Expressions RootsTech 2013
PPTX
Regular expressions using Python
PPTX
Java: Regular Expression
PPTX
Regular expression examples
Finaal application on regular expression
Textpad and Regular Expressions
Regular expressions
2.regular expressions
Basta mastering regex power
16 Java Regex
Regular expressions
Regular Expression (Regex) Fundamentals
Regular Expressions grep and egrep
Python advanced 2. regular expression in python
Regex Presentation
Looking for Patterns
Regular expressions
Regular Expression
Regex Basics
Regular Expressions 101
Introduction to Regular Expressions RootsTech 2013
Regular expressions using Python
Java: Regular Expression
Regular expression examples
Ad

Viewers also liked (7)

PPTX
Recreo clase 2 fernanda 1c
PDF
BSc Hons - Quantity Surveying
DOC
Cách làm đèn kéo quân “走馬燈”- the miss la sen revolving lantern
PPTX
Las apps en tipo presentacion
PDF
F I C+ + L P 05
PPTX
Co bot 발표자료
PDF
Climate Change and Tourism: Global Context
Recreo clase 2 fernanda 1c
BSc Hons - Quantity Surveying
Cách làm đèn kéo quân “走馬燈”- the miss la sen revolving lantern
Las apps en tipo presentacion
F I C+ + L P 05
Co bot 발표자료
Climate Change and Tourism: Global Context
Ad

Similar to Regular Expressions and You (20)

PDF
Regular expressions
PDF
Course 102: Lecture 13: Regular Expressions
PPTX
Bioinformatica p2-p3-introduction
PPT
Regex Experession with Regex functions o
PDF
Regular expressions
PDF
Maxbox starter20
PPT
PERL Regular Expression
ODP
Introduction To Regex in Lasso 8.5
PDF
Don't Fear the Regex - CapitalCamp/GovDays 2014
PDF
Regex startup
PDF
Construction of a predictive parsing table.pdf
PPT
Class 5 - PHP Strings
PPTX
[Compilers23] Lexical Analysis – Scanning Part I.pptx
PDF
2013 - Andrei Zmievski: Clínica Regex
PPT
Regular Expression in Action
DOCX
Python - Regular Expressions
PPTX
Regular Expressions in Stata
PPT
RegEx : Expressions and Parsing Examples
PDF
Don't Fear the Regex LSP15
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Regular expressions
Course 102: Lecture 13: Regular Expressions
Bioinformatica p2-p3-introduction
Regex Experession with Regex functions o
Regular expressions
Maxbox starter20
PERL Regular Expression
Introduction To Regex in Lasso 8.5
Don't Fear the Regex - CapitalCamp/GovDays 2014
Regex startup
Construction of a predictive parsing table.pdf
Class 5 - PHP Strings
[Compilers23] Lexical Analysis – Scanning Part I.pptx
2013 - Andrei Zmievski: Clínica Regex
Regular Expression in Action
Python - Regular Expressions
Regular Expressions in Stata
RegEx : Expressions and Parsing Examples
Don't Fear the Regex LSP15
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
MYSQL Presentation for SQL database connectivity
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Unlocking AI with Model Context Protocol (MCP)
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Weekly Chronicles - August'25 Week I
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx

Regular Expressions and You

  • 1. Regular Expressions and You An introduction to regular expressions. James I. Armes Web Developer, AllPlayers.com @jamesiarmes
  • 2. Email Validation Examples ^[w.%+-]+@[w.-]+.[A-Za-z]{2,4}$
  • 3. Email Validation Examples (?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?: [^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*|(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?: (?:rn)?[ t])*)*<(?:(?:rn)?[ t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+ (?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[ t])*)?(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?: (?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?: (?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?: [^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*>(?:(?:rn)?[ t])*)|(?:[^()<>@,;:".[] 000-031]+(?:(?: (?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)*:(?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(? =[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?: [^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)? [ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*|(?:[^()<>@,;:".[] 000-031]+ (?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)*<(?:(?:rn)?[ t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t]) +|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^ []r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?: (?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[ t])*)?(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?: (?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?: (?:rn)?[ t])*))*>(?:(?:rn)?[ t])*)(?:,s*(?:(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*) (?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?: [^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?: (?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*|(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?: (?:rn)?[ t]))*"(?:(?:rn)?[ t])*)*<(?:(?:rn)?[ t])*(?:@(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:. (?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*(?:,@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[ ["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*)*:(?:(?:rn)?[ t])*)?(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?: (?:rn)?[ t]))*"(?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|"(?:[^"r]|.|(?:(?:rn)?[ t]))*"(?: (?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?: [^()<>@,;:".[] 000-031]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:".[]]))|[([^[]r]|.)*](?:(?:rn)?[ t])*))*>(?:(?:rn)?[ t])*))*)?;s*)
  • 4. Types of Regular Expressions ● Simple Regular Expressions ● POSIX Basic Regular Expressions ● POSIX Extended Regular Expressions ● Perl Regular Expressions
  • 5. Simple Regular Expressions ● Traditional regular expressions. ● Not a standard. ● Support by some applications for backwards compatibility. ● Deprecated.
  • 6. POSIX Basic Regular Expressions ● Created to provide a common standard for Unix tools. ● Designed to be backwards compatible with traditional regular expressions. ● Adopted as the default syntax of many Unix tools. ● Some metacharacters require escaping.
  • 7. POSIX Extended Regular Expressions ● Adds some new metacharacters. ● Metacharacters do not require escaping. ● Dropped support for back references (n). ● Many Unix tools provide support with a command line argument (usually -E).
  • 8. Perl Regular Expressions ● Adds lazy quantification, named capture groups and recursive patterns. ● Adopted by many programming languages due to its power. ● Requires non-alphanumeric delimiters around expression. ● Other languages only implement a subset, so implementations vary.
  • 10. Basic Metacharacters . Match any single character. ^ Matches beginning of a string. $ Matches end of a string. | Matches the expression before or after (think ||).
  • 11. Character Classes [] Match any characters within the group. [^ ] Match any characters NOT within the group. [n-m] Match a range of characters. Examples: [A-Za-z0-9] [^G-Zg-z _]
  • 12. Shorthand Character Classes s Any whitespace character such as space, tab and newlines. Same as [nrt ] w Any word character. Same as [A-Za-z0-9_] d Any digit character. Same as [0-9] S, W, D Negated version of the above. Can be used inside character classes but could be confusing.
  • 13. Quantifiers * Match the preceding expression 0 or more times. + Match the preceding expression 1 or more times. ? Match the preceding expression 0 or 1 time. {m,n} Match the preceding expression at least m times but no more than n times. {m,} Match the preceding expression at least m times with no maximum. {,n} Match the preceding expression no more than n times with no minimum. {n} Match the preceding expression exactly n times.
  • 14. Lazy Quantifiers Standard Quantifiers are greedy. Example: Many programming courses start with a "Hello World" example. That would be "Hallo Welt" in German. "Hello .*" Many programming courses start with a "Hello World" example. That would be "Hallo Welt" in German.
  • 15. Lazy Quantifiers Use ? to make a quantifier lazy. Example: Many programming courses start with a "Hello World" example. That would be "Hallo Welt" in German. "Hello .*?" Many programming courses start with a "Hello World" example. That would be "Hallo Welt" in German.
  • 16. Grouping () Group the expression and capture the text. (?: ) Group the expression but DO NOT capture the text.
  • 17. Backreferences 1 through 9 reference previously captured text. Example: Many programming courses start with a "Hello World" example. 'Hello World' examples are extremely simple, especially when they just output "Hello World'. ('|")Hello World(1) Many programming courses start with a "Hello World" example. 'Hello World' examples are extremely simple, especially when they just output "Hello World'.
  • 18. Word Boundaries b matches the position between a word character (w) and a non-word character (W). Example: Hello World ob Hello| World
  • 19. Word Boundaries B matches the position between two word characters (ww). Example: Hello World oB Hello Wo|rld
  • 20. Lookaheads (?= ) matches the position directly before the expression is matched. Example: Hello World sounds better than "Hello Earth". Hello(?= World) Hello World sounds better than "Hello Earth".
  • 21. Lookbehinds (?<= ) matches the position directly after the expression is matched. Example: Hello World sounds better than "Hello Earth". (?<=")Hello Hello World sounds better than "Hello Earth".
  • 22. Lookaheads (?! ) matches the position directly before the expression is NOT matched. Example: Hello World sounds better than "Hello Earth". Hello(?! World) Hello World sounds better than "Hello Earth".
  • 23. Lookbehinds (?<! ) matches the position directly after the expression is NOT matched. Example: Hello World sounds better than "Hello Earth". (?<!")Hello Hello World sounds better than "Hello Earth".
  • 24. Conditionals (?(condition)then|else) ● condition must be a lookahead or a lookbehind. ● If condition is matched, then must match for the expression to pass. ● If condition is not matched, else must match for the expression to pass.
  • 25. Conditionals Example: Hello World sounds better than "Hello Earth". Hello (?(?<=World)World|Earth) Hello World sounds better than "Hello Earth". Hello (?(?<=People)People|Earth) Hello World sounds better than "Hello Earth".
  • 26. Modifiers i Case insensitive matching. s . matches newline characters. m ^ and $ match after and before newlines (respectively). x Whitespace within the expression is ignored unless escaped. g Match globally.
  • 27. Modifiers ● (?a) to turn modifiers on. ●(?-a) to turn modifiers off. Examples: (?i)WORLD(?-i) (?i-s)WORLD.(?s-i) (?i:WORLD)
  • 29. JavaScript ● RegExp object. – var expression = new RegExp('World', 'g'); – var expression = /World/g; ● String.match() ● String.replace() ● String.split()
  • 30. Perl ● if ($string =~ /regex/) ● $string =~ s/regex/replacement/ ● Regexp::Common – http://search.cpan.org/dist/Regexp-Common/ – Provides common expressions. – Examples: ● IP Address ● Credit Card Number ● Profanity
  • 31. PHP ● ereg vs. preg – preg uses Perl syntax. – ereg uses POSIX Extended syntax. – preg is much faster. – ereg has been deprecated as of PHP 5.3.
  • 32. PHP ● preg_match() ● preg_match_all() ● preg_replace() ● preg_split() ● preg_quote() ● http://www.php.net/manual/en/book.pcre.php ● http://php.net/manual/reference.pcre.pattern.modifiers.php
  • 33. Tools and Resources ● txt2regex - http://aurelio.net/txt2regex/ ● Reggy (mac) - http://reggyapp.com/ ● Patterns (mac) - http://krillapps.com/patterns/ ● Web based - http://regex.larsolavtorvik.com/ ● Regular-Expressions.info (reference) - http://www.regular-expressions.info/