SlideShare a Scribd company logo
(?<=REGEX)
PRIMER
D A L L A S H A C K E R S A S S O C I AT I O N
C H R I S M E R C E R – D H A @ Z A I M O R . C O M
DISCLAIMER
• IANARE: I am not a RegEx expert
• This is a crash course in what RegEx
is and what it can do
• Hopefully this will teach you some
new tricks to make your text
transforming bearable
DID YOU KNOW…
• Raise your hand if you’ve heard of RegEx!
• Put them DOWN if you DIDN’T know you could use RegEx to:
– Validate input (make sure someone put in a correctly formed email)
– Find text in code, in commands, and in text apps like vim, atom,
notepad++
– Find matches before or after other text (like finding a q without a u)
– Find text based on conditions (find A followed by T if it starts with C,
otherwise followed by S)
– Find AND REPLACE variable pieces of text (replace full names with
initials)
• If your hand is still up, go pick locks. You’re not learning anything
from me today. For the rest of you…
WTF IS REGEX?
• RegEx, or Regular Expressions, is a defined
“pattern” used for finding a specific string of text
• Wildcards on steroids
• You can find text that:
– Is before or after other text
– Is made up of a set of characters
– Is grouped together/saved to reference later
– Includes of tabs, spaces, new lines, special characters
• Several flavors/implementations, all “mostly”
compatible
VALIDATE/SEARCH
• Easiest, most well-known way to use RegEx
• Find a variable string of text, or validate that user input matches a required format
USE IT ANYWHERE
FIND TEXT IN PROXIMITY
• Lookahead (Positive or Negative)
– Find “bob” but only before “saget”
– Find “bob” but NOT before “saget”
• Lookbehind (Positive or Negative)
– Find “saget” but only after “bob”
– Find “saget” but NOT after “bob”
CONDITIONALS
• Home-grown syntax checking
• Assume anything starting with { must end with }, ( must end with ):
REPLACE
^([A-Z])(?:[a-z])*(?:s(?:([A-Z])(?:[a-z])*))?s(?:([A-Z])(?:[a-z])*)$
• Find (F)irst (optional (M)iddle) (L)ast, save initials to $1, $2, $3
– James Bond
– Rory Gilmore
– Johnny Appleseed
– George W Bush
– Jimmy Jo Bob
• Replace with “First: $1 Middle: $2 Last: $3":
– First: J Middle: Last: B
– First: R Middle: Last: G
– First: J Middle: Last: A
– First: G Middle: W Last: B
– First: J Middle: J Last: B
HOW DOES THIS HELP AVERAGE JOE?
What if…
I’m a teacher, and I want to take an old exam with 100 questions that looked like this:
1. Question
Answer 1
Answer 2
Answer 3
Answer 4
And format it for importing to another system like this:
Question 1|Question|A. Answer 1|B. Answer 2|C. Answer 3|D. Answer 4
Question 2|Question|A. Answer 1|B. Answer 2|C. Answer 3|D. Answer 4
RESOURCES
• https://www.regular-expressions.info/tutorial.html
• https://regex101.com/
• http://www.regexe.com
• https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines
• This Slideshow:

More Related Content

Similar to Regex Primer

Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
Akhil Kaushik
 
CiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex Presentation
ThreatReel Podcast
 
Regular Expression Crash Course
Regular Expression Crash CourseRegular Expression Crash Course
Regular Expression Crash Course
Imran Qasim
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式ji guang
 
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
Bertram Ludäscher
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
keeyre
 
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
Sandy Smith
 
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
Sandy Smith
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
Keith Wright
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
ssuser8779cd
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
primeteacher32
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat Sheet
SydneyJohnson57
 
Regexp secrets
Regexp secretsRegexp secrets
Regexp secrets
Hiro Asari
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
Geoffrey Dunn
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and YouJames Armes
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
regex.ppt
regex.pptregex.ppt
regex.ppt
ansariparveen06
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
Md. Shafiuzzaman Hira
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Paolo Carrasco Mori
 

Similar to Regex Primer (20)

Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
CiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex Presentation
 
Regular Expression Crash Course
Regular Expression Crash CourseRegular Expression Crash Course
Regular Expression Crash Course
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
 
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
 
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
 
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
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat Sheet
 
Regexp secrets
Regexp secretsRegexp secrets
Regexp secrets
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Reg EX
Reg EXReg EX
Reg EX
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
regex.ppt
regex.pptregex.ppt
regex.ppt
 
Regular expressions using Python
Regular expressions using PythonRegular expressions using Python
Regular expressions using Python
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 

Recently uploaded

Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 

Recently uploaded (20)

Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 

Regex Primer

  • 1. (?<=REGEX) PRIMER D A L L A S H A C K E R S A S S O C I AT I O N C H R I S M E R C E R – D H A @ Z A I M O R . C O M
  • 2. DISCLAIMER • IANARE: I am not a RegEx expert • This is a crash course in what RegEx is and what it can do • Hopefully this will teach you some new tricks to make your text transforming bearable
  • 3. DID YOU KNOW… • Raise your hand if you’ve heard of RegEx! • Put them DOWN if you DIDN’T know you could use RegEx to: – Validate input (make sure someone put in a correctly formed email) – Find text in code, in commands, and in text apps like vim, atom, notepad++ – Find matches before or after other text (like finding a q without a u) – Find text based on conditions (find A followed by T if it starts with C, otherwise followed by S) – Find AND REPLACE variable pieces of text (replace full names with initials) • If your hand is still up, go pick locks. You’re not learning anything from me today. For the rest of you…
  • 4. WTF IS REGEX? • RegEx, or Regular Expressions, is a defined “pattern” used for finding a specific string of text • Wildcards on steroids • You can find text that: – Is before or after other text – Is made up of a set of characters – Is grouped together/saved to reference later – Includes of tabs, spaces, new lines, special characters • Several flavors/implementations, all “mostly” compatible
  • 5. VALIDATE/SEARCH • Easiest, most well-known way to use RegEx • Find a variable string of text, or validate that user input matches a required format
  • 7. FIND TEXT IN PROXIMITY • Lookahead (Positive or Negative) – Find “bob” but only before “saget” – Find “bob” but NOT before “saget” • Lookbehind (Positive or Negative) – Find “saget” but only after “bob” – Find “saget” but NOT after “bob”
  • 8. CONDITIONALS • Home-grown syntax checking • Assume anything starting with { must end with }, ( must end with ):
  • 9. REPLACE ^([A-Z])(?:[a-z])*(?:s(?:([A-Z])(?:[a-z])*))?s(?:([A-Z])(?:[a-z])*)$ • Find (F)irst (optional (M)iddle) (L)ast, save initials to $1, $2, $3 – James Bond – Rory Gilmore – Johnny Appleseed – George W Bush – Jimmy Jo Bob • Replace with “First: $1 Middle: $2 Last: $3": – First: J Middle: Last: B – First: R Middle: Last: G – First: J Middle: Last: A – First: G Middle: W Last: B – First: J Middle: J Last: B
  • 10. HOW DOES THIS HELP AVERAGE JOE? What if… I’m a teacher, and I want to take an old exam with 100 questions that looked like this: 1. Question Answer 1 Answer 2 Answer 3 Answer 4 And format it for importing to another system like this: Question 1|Question|A. Answer 1|B. Answer 2|C. Answer 3|D. Answer 4 Question 2|Question|A. Answer 1|B. Answer 2|C. Answer 3|D. Answer 4
  • 11. RESOURCES • https://www.regular-expressions.info/tutorial.html • https://regex101.com/ • http://www.regexe.com • https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines • This Slideshow: