SlideShare a Scribd company logo
Regular Expressions
        101
       Raj Kissu
What are Regular
  Expressions?
They describe
patterns in strings
These patterns can
 be used to modify
      strings
Invented by Stephen
    Cole Kleene
Idea of RegEx dates
 back to the 1950s
Today, they come in
different “flavors”
PCRE, POSIX Basic &
Extended RegEx, ECMA
  RegEx and loads
       more!
NOTE: RegEx
“flavors” are not
   consistent in
  implementation
Different “flavors”
       behave
  differently ...
So know your
“flavor” before you
       use it!
Why Use It?
Cos it’s an
important tool in
  every coder’s
     arsenal!
Ok, but still ...
  Why use it???
It’s Short, Sweet &
   F***ing Fast!
It can match just
 about anything!
It makes changing
 large amounts of
  repetitive text
    trivial ...
... as long as you
can “see” patterns
It Makes You Awesome
- in a Geeky way :)
Before RegEx Mastery
After RegEx Mastery
Readily Available

Support in programming languages:
JavaScript, PHP, PERL, C/C++,etc

Command-line: grep, awk, sed

Text-editors: VIM, emacs, Notepad++

IDEs: Aptana, Netbeans, Visual
Studio .NET
RegEx Basics
NOTE : Using ECMA (JavaScript) RegEx Flavor!
Characters

standard characters

  letters : A to Z, a to z

  numbers : 0 to 9

  symbols : !,@,#,%,& etc

Matched literally!
Meta Characters


Special characters : )(][^$.|?*+

To match as literals, escape them
with a backslash
Character Classes
Matches one and ONLY one character in
a set of characters

[Aa]   : matches either ‘A’ or ‘a’

[a-z] : matches any of the lowercase
alphabets in the specified range ONCE

[^Aa] : matches anything but ‘A’ and
‘a’
Character Classes

Metacharacters may behave differently
within character classes

[^red] : matches anything but ‘r’,
‘e’ and ‘d’

[r^ed] : matches only ‘r’, ‘^’, ‘e’
or ‘d’
Shorthand Classes

d, [0-9]: digits

w, [da-zA-Z_]: alphanumeric or _

s or [ t(?:n|rn)] : whitespace

D, W, S : the above BUT negated
The Dot Character

The Dot (.) character matches any
single character BUT the newline

Synonymous with [^n] (UNIX/Linux/
Mac)

as well as [^rn] (Windows)

Use it sparingly - it’s expensive
Alternation

Using a pipe |, match either the left
or right side of the pattern

bear|tiger : matches a string that
contains either “bear” or “tiger”

pedo(bea|tige)r : matches a string
that contains either “pedobear” or
“pedotiger”
Quantifiers

{n} : matches exactly n times

{n,} : matches n or more times

{n,m} : matches between n and m times

* : same as {0,}

+ : same as {1,}

? : same as {0,1}
Quantifiers

Quantifiers are greedy

<.+> : matches “<div>holy RegEx,
Batman!</div>” instead of stopping at
“<div>”

Add a ? to make it lazy

<.+?> : stops at “<div>” in
“<div>holy regex!</div>”
Anchors

Matches positions instead of
characters

^ : matches the beginning of a string

$ : matches the end of a string

b : matches between a w and a token
that’s not a w
Groupings
Placing parentheses around tokens
groups them together : /nyan(cat)/

It also provides a
backreference :    /(cat)1/ matches
“cat”

OR if you don’t want a
backreference :   /(?:nyan)(cat)1/
matches “nyancatcat” and not
“nyancatnyan”
Lookahead

Positive Lookahead

  Iron(?=man) : matches “Iron” only
  if it is followed by “man”

Negative Lookahead

  Iron(?!man) : matches “Iron” only
  if it is not followed by “man”
Lookbehind

Positive Lookbehind

  (?<=Iron)man : matches “man” only
  if it is preceded by “Iron”

Negative Lookbehind

  (?<!Iron)man : matches “man” only
  if it is not preceded by “Iron”
Modifiers

alter behavior of the matching mode
(differs between tools)

/i : case-insensitive match

/m : Multi-line mode

/g : affects all possible matches,
not just the first
Q & A
Resources

Mastering Regular Expressions -
Jeffrey E.F. Friedl

http://www.regex.info

http://www.regular-expressions.info

http://www.rubular.com
Thank You!

More Related Content

PPTX
Regular Expression
PPTX
Regular expressions
ODP
Regex Presentation
PPTX
Regular Expressions 101 Introduction to Regular Expressions
PPTX
Regular Expression (Regex) Fundamentals
PPT
Regex Basics
PPTX
Introduction to Regular Expressions
PPT
Regular Expressions
Regular Expression
Regular expressions
Regex Presentation
Regular Expressions 101 Introduction to Regular Expressions
Regular Expression (Regex) Fundamentals
Regex Basics
Introduction to Regular Expressions
Regular Expressions

What's hot (20)

PDF
Regex - Regular Expression Basics
PPTX
Regular expressions
PDF
An Introduction to Regular expressions
PPTX
Php functions
PPT
Javascript
PDF
Regular expression
PPT
Regular expressions
PDF
R code for data manipulation
PPT
PPTX
Css ppt
PPT
One Dimensional Array
PDF
JavaScript - Chapter 5 - Operators
PPT
Regular Expressions grep and egrep
PDF
Callback Function
PPTX
Php operators
PDF
Php introduction
PPTX
presentation in html,css,javascript
PPTX
Php.ppt
Regex - Regular Expression Basics
Regular expressions
An Introduction to Regular expressions
Php functions
Javascript
Regular expression
Regular expressions
R code for data manipulation
Css ppt
One Dimensional Array
JavaScript - Chapter 5 - Operators
Regular Expressions grep and egrep
Callback Function
Php operators
Php introduction
presentation in html,css,javascript
Php.ppt
Ad

Similar to Regular Expressions 101 (20)

PDF
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
PDF
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
PDF
Don't Fear the Regex - CapitalCamp/GovDays 2014
DOCX
Quick start reg ex
PDF
Regular expressions
PPTX
Regular Expression
PPT
Introduction to Regular Expressions
PDF
Hw1 rubycalisthenics
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
PPTX
Regex lecture
PDF
Don't Fear the Regex LSP15
ODP
Regex Presentation
PPT
regular-expressions lecture 28-string regular expression
PPT
Introduction to Regular Expressions RootsTech 2013
PDF
Don't Fear the Regex WordCamp DC 2017
PPT
Php Chapter 4 Training
ODP
Looking for Patterns
ODP
Regular Expressions and You
PDF
Don't Fear the Regex - Northeast PHP 2015
PDF
Introduction_to_Regular_Expressions_in_R
Week-2: Theory & Practice of Data Cleaning: Regular Expressions in Practice
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
Don't Fear the Regex - CapitalCamp/GovDays 2014
Quick start reg ex
Regular expressions
Regular Expression
Introduction to Regular Expressions
Hw1 rubycalisthenics
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Regex lecture
Don't Fear the Regex LSP15
Regex Presentation
regular-expressions lecture 28-string regular expression
Introduction to Regular Expressions RootsTech 2013
Don't Fear the Regex WordCamp DC 2017
Php Chapter 4 Training
Looking for Patterns
Regular Expressions and You
Don't Fear the Regex - Northeast PHP 2015
Introduction_to_Regular_Expressions_in_R
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation_ Review paper, used for researhc scholars
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
MYSQL Presentation for SQL database connectivity
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Diabetes mellitus diagnosis method based random forest with bat algorithm
Digital-Transformation-Roadmap-for-Companies.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Review of recent advances in non-invasive hemoglobin estimation

Regular Expressions 101

Editor's Notes