SlideShare a Scribd company logo
^[Rr]egular [Ee]xpressions$ Introduction
Vocabulary Regular expression / Regex / Regexp Regex is pronounced Reg (as in register) Ex (as in FedEx) Matching Regex matches a string means it matches  in  a string
Regular Expressions Composed of two types of characters Metacharacters / Special characters * ? ^ $ . [ ] Literal characters a  b  c  d
Egrep tool Allows you to use Regular Expressions to find words that match Available for Macs, PCs and Linux cat /usr/share/dict/words | egrep ‘…’ See  http://regex.info/egrep.html  if you don’t have it preinstalled
My first regex cat /usr/share/dict/words | egrep ‘cat’ Matches any words  with a ‘c’  followed by an ‘a’  followed by a ‘t’ bobcat cat catwalk scatter Simple regex, only uses Literal chars
Metacharacters:  ^ and $ ^ matches the beginning of a line $ matches the end of a line ^cat  (start of line followed by ‘c’ then ‘a’ then ‘t’) cat catwalk cat$ (‘c’ followed by ‘a’ then ‘t’ followed by EOL) bobcat cat ^cat$ (start of line followed by ‘c’ then ‘a’  then ‘t’ then EOL) cat
How to read regex Read each character one at a time ^bat Start of line followed by ‘b’ then ‘a’ then ‘t’ rat$ ‘ r’ then ‘a’ then ‘t’ followed by end of line ^dog$ Start of line followed by ‘d’ then ‘o’ then ‘g’ then EOL
More simple regex’s ^ Start of line ^$ Start of line followed by end of line $ End of line ^foot$ Start of line followed by ‘f’ then ‘o’ then ‘o’ then ‘t’ followed by EOL
Character Classes [ ] Matches one of the characters in the [ ] [ae] Matches ‘a’ or ‘e’ [aeiouy] Matches any vowel ^gr[ae]y$ Start of line followed by ‘g’ then ‘r’ then ‘a’ or ‘e’ then ‘y’ followed by end of line grey or gray
Character Classes cont. [Ss] Matches upper or lower case ‘S’ [123456] Matches any of the digits listed [Hh][123456] Matches H1, h2, h3, H4, etc
Special characters in [ ]’s - (dash) references a range [1-6] is the same as [123456] [a-f] is the same as [abcdef] Ranges can be mixed with literals [0-9a-fA-F_!.?] Any digit, upper or lower case ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, underscore, exclamation, period or question mark
Negated character class [^ ] ^ inside of [ ] means “not any of these” [^1-6] Any character other than 1, 2, 3, 4, 5, 6 [^a-fA-F] Any character other than A-F (upper or lower) The ^ must be the first character inside [ ] [^c] (Matches anything but ‘c’) [c^] (Matches a ‘c’ or ‘^’)
Translating regex practice List of words that have ‘q’ followed by a character other than ‘u’ q[^u] List of words with ‘f’ followed by an ‘i’ or ‘o’ followed by ‘r’ then ‘e’ f[io]re Line starts with ‘Qu’ or ‘qu’ followed by an ‘e’ followed by any letter between ‘p’ and ‘t’ ^[Qq]ue[p-t]
Metacharacter:  .  (dot) Matches any character c.t  ‘ c’ followed by any character followed by ‘t’ cat cot c8t Period inside of [ ]’s matches a period [a.c] Matches ‘a’, ‘.’ or ‘c’
Periods cont. 03.19.76 Matches ‘03’ followed by a char then ‘19’ then any char then ‘76’ 03-19-76 03/19/76 03.19.76 03 19 76 03 3 19 8 76
Alternatives:  | (pipe) Pipes allow you to specify alternatives grey|gray Matches on grey or gray Use parentheses to constrain alternatives gr(e|a)y Within [ ]’s, | is a normal character [a|b] Matches ‘a’ or ‘|’ or ‘b’
Pipes (cont.) Use parenthesis to constrain gre|ay matches ‘gre’ or ‘ay’ gr(e|a)y matches ‘gr’ followed by ‘e’ or ‘a’ then ‘y’
Regex practice Match “First Street” or “1st street” (First|1st) [Ss]treet (Fir|1)st [Ss]treet These are equivalent, which is better? Match “toothbrush” or “hairbrush” (tooth|hair)brush
^ or $ and alternation Be careful when using ^ or $ with alternation ^From|Subject|Date: Start of line followed by From OR Subject OR Date: ^(From|Subject|Date): Start of line followed by ‘From’ or ‘Subject’ or ‘Date’ followed by ‘:’ Safer to use ()’s to group your alternates
Case insensitive match Matches are case sensitive by default [Ff]rom will match From but not FRom Use egrep’s -i option to do a case insensitive match Most languages have a case insensitive match as well
Quantifiers: ? ? metacharacter means optional colou?r matches color or colour ‘ c’ then ‘o’ then ‘l’ then ‘o’ then optionally ‘u’ then ‘r’ Match July or Jul and fourth, 4th and 4 (July|Jul) (fourth|4th|4) July? (fourth|4th|4) July? (fourth|4(th)?)
Quantifiers: + and * + (plus)  One or more of the previous item * (star) Zero or more of the previous item b[0-9]*a ba b9999a b999999999999999a
Summary of Quantifiers Minimum Required Maximum to try Meaning ? none 1 zero or one occurrence * none no limit zero or more occurrences + 1 no limit one or more occurrences
Escaping metacharacters Use \ (backslash) to escape metacharacters \. matches ‘.’ . matches any character c.t matches cat c\.t does not match cat \(cat\) matches ‘(cat)’ not ‘cat’
More practice Match chat, cat, chart ch?ar?t c[h]?a[r]?t Start of line then M then one or more ‘a’ followed by ‘st’ and zero or more ‘b’ ^M[a]+st[b]* Lines ending with one or more ‘c’ followed by a ‘t’ then zero or one ‘e’ [c]+t[e]*$
More practice ^[Mm][^a-np-z]ney$ Start of line then ‘M’ or ‘m’ then any character not a-n and p-z then ‘ney’ followed by end of line Money, money, m3ney ^be.*(bob|ted)$ Start of line followed by ‘be’ followed by zero or more characters followed by ‘bob’ or ‘ted’ followed by end of line
More practice Match truck, firetruck but not dumptruck ^(fire)?truck$ $0.99, $599.95, $1000.45, $5000 \$[0-9]+(\.[0-9][0-9])?$ 404-555-1212, 404.555.1212, (404) 555-1212 ^[()0-9]+.[0-9]+.[0-9]+$
Ad

Recommended

Regular expression
Regular expression
Larry Nung
 
Regular Expressions Cheat Sheet
Regular Expressions Cheat Sheet
Akash Bisariya
 
Regular Expressions
Regular Expressions
Satya Narayana
 
Regular expressions
Regular expressions
Eran Zimbler
 
Introduction to Regular Expressions
Introduction to Regular Expressions
Matt Casto
 
An Introduction to Regular expressions
An Introduction to Regular expressions
Yamagata Europe
 
Regular Expressions 101
Regular Expressions 101
Raj Rajandran
 
Regular expressions quick reference
Regular expressions quick reference
jvinhit
 
Regex startup
Regex startup
PayPal
 
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
bilcorry
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Software development life cycle yazılım geliştirme yaşam döngüsü
Software development life cycle yazılım geliştirme yaşam döngüsü
Mesut Günes
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
Mesut Günes
 
Regular Expressions Demystified
Regular Expressions Demystified
Philip Tellis
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
Ahmed El-Arabawy
 
Regular Expressions grep and egrep
Regular Expressions grep and egrep
Tri Truong
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
Shaun Griffith
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
azzamhadeel89
 
Regular Expression
Regular Expression
Mahzad Zahedi
 
Basta mastering regex power
Basta mastering regex power
Max Kleiner
 
Andrei's Regex Clinic
Andrei's Regex Clinic
Andrei Zmievski
 
Regular Expressions
Regular Expressions
Akhil Kaushik
 
Python (regular expression)
Python (regular expression)
Chirag Shetty
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
Introduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 

More Related Content

Viewers also liked (7)

Regex startup
Regex startup
PayPal
 
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
bilcorry
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Software development life cycle yazılım geliştirme yaşam döngüsü
Software development life cycle yazılım geliştirme yaşam döngüsü
Mesut Günes
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
Mesut Günes
 
Regular Expressions Demystified
Regular Expressions Demystified
Philip Tellis
 
Regex startup
Regex startup
PayPal
 
Introduction To Regex in Lasso 8.5
Introduction To Regex in Lasso 8.5
bilcorry
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Software development life cycle yazılım geliştirme yaşam döngüsü
Software development life cycle yazılım geliştirme yaşam döngüsü
Mesut Günes
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
Mesut Günes
 
Regular Expressions Demystified
Regular Expressions Demystified
Philip Tellis
 

Similar to Regex Intro (20)

Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
Ahmed El-Arabawy
 
Regular Expressions grep and egrep
Regular Expressions grep and egrep
Tri Truong
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
Shaun Griffith
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
azzamhadeel89
 
Regular Expression
Regular Expression
Mahzad Zahedi
 
Basta mastering regex power
Basta mastering regex power
Max Kleiner
 
Andrei's Regex Clinic
Andrei's Regex Clinic
Andrei Zmievski
 
Regular Expressions
Regular Expressions
Akhil Kaushik
 
Python (regular expression)
Python (regular expression)
Chirag Shetty
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
Introduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Python - Lecture 7
Python - Lecture 7
Ravi Kiran Khareedi
 
Regular Expression Crash Course
Regular Expression Crash Course
Imran Qasim
 
Maxbox starter20
Maxbox starter20
Max Kleiner
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
Ben Brumfield
 
Lecture_4.pdf
Lecture_4.pdf
SteveHuang50
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
Ahmed El-Arabawy
 
Regular Expressions grep and egrep
Regular Expressions grep and egrep
Tri Truong
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
Shaun Griffith
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
azzamhadeel89
 
Basta mastering regex power
Basta mastering regex power
Max Kleiner
 
Python (regular expression)
Python (regular expression)
Chirag Shetty
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Introduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Regular Expression Crash Course
Regular Expression Crash Course
Imran Qasim
 
Maxbox starter20
Maxbox starter20
Max Kleiner
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
Ben Brumfield
 
Ad

More from Jason Noble (17)

Intro to TDD and BDD
Intro to TDD and BDD
Jason Noble
 
Davinci git brown_bag
Davinci git brown_bag
Jason Noble
 
Rspec 101
Rspec 101
Jason Noble
 
Dash of ajax
Dash of ajax
Jason Noble
 
jQuery Intro
jQuery Intro
Jason Noble
 
Intro to Rails Give Camp Atlanta
Intro to Rails Give Camp Atlanta
Jason Noble
 
Google apps
Google apps
Jason Noble
 
Smarter cart
Smarter cart
Jason Noble
 
Cart creation-101217222728-phpapp01
Cart creation-101217222728-phpapp01
Jason Noble
 
Catalog display
Catalog display
Jason Noble
 
Validation unit testing
Validation unit testing
Jason Noble
 
Creating the application
Creating the application
Jason Noble
 
Capistrano
Capistrano
Jason Noble
 
Atlanta Pm Git 101
Atlanta Pm Git 101
Jason Noble
 
Git101
Git101
Jason Noble
 
Git Atlrug
Git Atlrug
Jason Noble
 
Git102
Git102
Jason Noble
 
Intro to TDD and BDD
Intro to TDD and BDD
Jason Noble
 
Davinci git brown_bag
Davinci git brown_bag
Jason Noble
 
Intro to Rails Give Camp Atlanta
Intro to Rails Give Camp Atlanta
Jason Noble
 
Cart creation-101217222728-phpapp01
Cart creation-101217222728-phpapp01
Jason Noble
 
Validation unit testing
Validation unit testing
Jason Noble
 
Creating the application
Creating the application
Jason Noble
 
Atlanta Pm Git 101
Atlanta Pm Git 101
Jason Noble
 
Ad

Recently uploaded (20)

A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 

Regex Intro

  • 2. Vocabulary Regular expression / Regex / Regexp Regex is pronounced Reg (as in register) Ex (as in FedEx) Matching Regex matches a string means it matches in a string
  • 3. Regular Expressions Composed of two types of characters Metacharacters / Special characters * ? ^ $ . [ ] Literal characters a b c d
  • 4. Egrep tool Allows you to use Regular Expressions to find words that match Available for Macs, PCs and Linux cat /usr/share/dict/words | egrep ‘…’ See http://regex.info/egrep.html if you don’t have it preinstalled
  • 5. My first regex cat /usr/share/dict/words | egrep ‘cat’ Matches any words with a ‘c’ followed by an ‘a’ followed by a ‘t’ bobcat cat catwalk scatter Simple regex, only uses Literal chars
  • 6. Metacharacters: ^ and $ ^ matches the beginning of a line $ matches the end of a line ^cat (start of line followed by ‘c’ then ‘a’ then ‘t’) cat catwalk cat$ (‘c’ followed by ‘a’ then ‘t’ followed by EOL) bobcat cat ^cat$ (start of line followed by ‘c’ then ‘a’ then ‘t’ then EOL) cat
  • 7. How to read regex Read each character one at a time ^bat Start of line followed by ‘b’ then ‘a’ then ‘t’ rat$ ‘ r’ then ‘a’ then ‘t’ followed by end of line ^dog$ Start of line followed by ‘d’ then ‘o’ then ‘g’ then EOL
  • 8. More simple regex’s ^ Start of line ^$ Start of line followed by end of line $ End of line ^foot$ Start of line followed by ‘f’ then ‘o’ then ‘o’ then ‘t’ followed by EOL
  • 9. Character Classes [ ] Matches one of the characters in the [ ] [ae] Matches ‘a’ or ‘e’ [aeiouy] Matches any vowel ^gr[ae]y$ Start of line followed by ‘g’ then ‘r’ then ‘a’ or ‘e’ then ‘y’ followed by end of line grey or gray
  • 10. Character Classes cont. [Ss] Matches upper or lower case ‘S’ [123456] Matches any of the digits listed [Hh][123456] Matches H1, h2, h3, H4, etc
  • 11. Special characters in [ ]’s - (dash) references a range [1-6] is the same as [123456] [a-f] is the same as [abcdef] Ranges can be mixed with literals [0-9a-fA-F_!.?] Any digit, upper or lower case ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, underscore, exclamation, period or question mark
  • 12. Negated character class [^ ] ^ inside of [ ] means “not any of these” [^1-6] Any character other than 1, 2, 3, 4, 5, 6 [^a-fA-F] Any character other than A-F (upper or lower) The ^ must be the first character inside [ ] [^c] (Matches anything but ‘c’) [c^] (Matches a ‘c’ or ‘^’)
  • 13. Translating regex practice List of words that have ‘q’ followed by a character other than ‘u’ q[^u] List of words with ‘f’ followed by an ‘i’ or ‘o’ followed by ‘r’ then ‘e’ f[io]re Line starts with ‘Qu’ or ‘qu’ followed by an ‘e’ followed by any letter between ‘p’ and ‘t’ ^[Qq]ue[p-t]
  • 14. Metacharacter: . (dot) Matches any character c.t ‘ c’ followed by any character followed by ‘t’ cat cot c8t Period inside of [ ]’s matches a period [a.c] Matches ‘a’, ‘.’ or ‘c’
  • 15. Periods cont. 03.19.76 Matches ‘03’ followed by a char then ‘19’ then any char then ‘76’ 03-19-76 03/19/76 03.19.76 03 19 76 03 3 19 8 76
  • 16. Alternatives: | (pipe) Pipes allow you to specify alternatives grey|gray Matches on grey or gray Use parentheses to constrain alternatives gr(e|a)y Within [ ]’s, | is a normal character [a|b] Matches ‘a’ or ‘|’ or ‘b’
  • 17. Pipes (cont.) Use parenthesis to constrain gre|ay matches ‘gre’ or ‘ay’ gr(e|a)y matches ‘gr’ followed by ‘e’ or ‘a’ then ‘y’
  • 18. Regex practice Match “First Street” or “1st street” (First|1st) [Ss]treet (Fir|1)st [Ss]treet These are equivalent, which is better? Match “toothbrush” or “hairbrush” (tooth|hair)brush
  • 19. ^ or $ and alternation Be careful when using ^ or $ with alternation ^From|Subject|Date: Start of line followed by From OR Subject OR Date: ^(From|Subject|Date): Start of line followed by ‘From’ or ‘Subject’ or ‘Date’ followed by ‘:’ Safer to use ()’s to group your alternates
  • 20. Case insensitive match Matches are case sensitive by default [Ff]rom will match From but not FRom Use egrep’s -i option to do a case insensitive match Most languages have a case insensitive match as well
  • 21. Quantifiers: ? ? metacharacter means optional colou?r matches color or colour ‘ c’ then ‘o’ then ‘l’ then ‘o’ then optionally ‘u’ then ‘r’ Match July or Jul and fourth, 4th and 4 (July|Jul) (fourth|4th|4) July? (fourth|4th|4) July? (fourth|4(th)?)
  • 22. Quantifiers: + and * + (plus) One or more of the previous item * (star) Zero or more of the previous item b[0-9]*a ba b9999a b999999999999999a
  • 23. Summary of Quantifiers Minimum Required Maximum to try Meaning ? none 1 zero or one occurrence * none no limit zero or more occurrences + 1 no limit one or more occurrences
  • 24. Escaping metacharacters Use \ (backslash) to escape metacharacters \. matches ‘.’ . matches any character c.t matches cat c\.t does not match cat \(cat\) matches ‘(cat)’ not ‘cat’
  • 25. More practice Match chat, cat, chart ch?ar?t c[h]?a[r]?t Start of line then M then one or more ‘a’ followed by ‘st’ and zero or more ‘b’ ^M[a]+st[b]* Lines ending with one or more ‘c’ followed by a ‘t’ then zero or one ‘e’ [c]+t[e]*$
  • 26. More practice ^[Mm][^a-np-z]ney$ Start of line then ‘M’ or ‘m’ then any character not a-n and p-z then ‘ney’ followed by end of line Money, money, m3ney ^be.*(bob|ted)$ Start of line followed by ‘be’ followed by zero or more characters followed by ‘bob’ or ‘ted’ followed by end of line
  • 27. More practice Match truck, firetruck but not dumptruck ^(fire)?truck$ $0.99, $599.95, $1000.45, $5000 \$[0-9]+(\.[0-9][0-9])?$ 404-555-1212, 404.555.1212, (404) 555-1212 ^[()0-9]+.[0-9]+.[0-9]+$