SlideShare a Scribd company logo
1 of 11
(?<=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

CiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationThreatReel Podcast
 
Regular Expression Crash Course
Regular Expression Crash CourseRegular Expression Crash Course
Regular Expression Crash CourseImran 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 PracticeBertram Ludäscher
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionskeeyre
 
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 2015Sandy 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 2017Sandy Smith
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for PatternsKeith Wright
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsssuser8779cd
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat SheetSydneyJohnson57
 
Regexp secrets
Regexp secretsRegexp secrets
Regexp secretsHiro Asari
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007Geoffrey 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
 

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

Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch TuesdayIvanti
 
How to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in PakistanHow to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in Pakistandanishmna97
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Paige Cruz
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data SciencePaolo Missier
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfAnubhavMangla3
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdfMuhammad Subhan
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)Wonjun Hwang
 
Navigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiNavigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiRaviKumarDaparthi
 

Recently uploaded (20)

Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
How to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in PakistanHow to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in Pakistan
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)CORS (Kitworks Team Study 양다윗 발표자료 240510)
CORS (Kitworks Team Study 양다윗 발표자료 240510)
 
Navigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi DaparthiNavigating the Large Language Model choices_Ravi Daparthi
Navigating the Large Language Model choices_Ravi Daparthi
 

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: