SlideShare a Scribd company logo
1 of 40
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

What's hot

Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raj Gupta
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
Harit Kothari
 

What's hot (20)

Regex - Regular Expression Basics
Regex - Regular Expression BasicsRegex - Regular Expression Basics
Regex - Regular Expression Basics
 
Introducing Regular Expressions
Introducing Regular ExpressionsIntroducing Regular Expressions
Introducing Regular Expressions
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Advanced regular expressions
Advanced regular expressionsAdvanced regular expressions
Advanced regular expressions
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions tutorial for SEO & Website Analysis
Regular expressions tutorial for SEO & Website AnalysisRegular expressions tutorial for SEO & Website Analysis
Regular expressions tutorial for SEO & Website Analysis
 
Xml namespace
Xml namespaceXml namespace
Xml namespace
 
Properties of relations
Properties of relationsProperties of relations
Properties of relations
 
Relations and Its Applications
Relations and Its ApplicationsRelations and Its Applications
Relations and Its Applications
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
 
Css box-model
Css box-modelCss box-model
Css box-model
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Input And Output
 Input And Output Input And Output
Input And Output
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Javascript Arrow function
Javascript Arrow functionJavascript Arrow function
Javascript Arrow function
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 

Similar to Regular Expressions 101

Hw1 rubycalisthenics
Hw1 rubycalisthenicsHw1 rubycalisthenics
Hw1 rubycalisthenics
shelton88
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
arnolambert
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
Ben Brumfield
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
James Armes
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
 

Similar to Regular Expressions 101 (20)

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
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
 
Quick start reg ex
Quick start reg exQuick start reg ex
Quick start reg ex
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Hw1 rubycalisthenics
Hw1 rubycalisthenicsHw1 rubycalisthenics
Hw1 rubycalisthenics
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Regular Expressions 101

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n