SlideShare a Scribd company logo
REGULAR
EXPRESSIONS
intro to regex
Pattern Matching
Powerful and widely applicable technique used
across various programming languages.
Everything can be written using characters
⇒ use regex to search for a pattern of characters!
Uses Cases:
● validate user input (emails, passwords)
● search text/code (vscode), replace/rename
● query databases
● extracting info from text (incl. web scraping)
● data massaging from raw
● File Renaming Web directives (Apache)
● interact with the Unix shell
● refactor code
Why it can be intimidating:
Regex Basics
/ open / close
g after regex to make the search global (don’t stop!)
i after regex to make the search case-insensitive
m after regex to perform multiline matching
/unicorn/ literal exact match
 escape special characters! ^.[$()|*+?{ tnr
click me!
3/regex/gimflags
. any character except newline
d digit character
D non-digit character
w “word” character (alphanumeric or _)
W non-word character
s whitespace character (space, tab, newline,
carriage return...)
S not whitespace character
Metacharacters
Anchors — ^text$
^ line start with (multiline flag)
$ line end with
/^The end$/gim
^The matches any string that starts with The
end$ matches a string that ends with end
^The end$ starts and ends with The end
Boundaries — b
b word boundary
B not a word
Quantifiers — *, +, ? and {}
* 0+ times (optional)
+ 1+ times (1 required)
? 0-1 times (optional) *lazy
{#} # number of times
{2,} 2+ times
{2,5} 2-5 times
0(?=abc) match 0 only if followed by “abc” vs 0(?!abc)
(?<=abc)0 match 0 only if preceded by “abc” vs (?<!abc)0
/hel{2}o/i;
hello hello Helo helllo
/hel{2,4}o/i;
hello hellllo Helo helllo
/hel{2,}o/i; (2+)
hello helllllo helo helo
[A-Z] uppercase characters (in range) a-z
[a-z] lowercase characters (in range) a-z
[A-Za-z] any letter
[aeiou] either a, e, i, o or u
[0-9] match any digits 0-9 (specific numbers [1-3] vs d)
Character Sets — []
/gr[ae]y/i ⇒
Third character must be “a” or “e”
[^aeiou] find any character not a, e, i, o or u
[^2-4] match any digits not 2, 3, 4
Grouping — ()
a(bc){2,5} Parens create a capturing group of “bc”
⇒ matches “a” followed by 2-5 “bc”s
| or ⇒ (x|y)==[xy] both matches x or y
(?:ab) match “ab” but don’t remember (“capture”) it
(demo|example)[0-9]+
demo1 demoexample2 example4 demo 9
([0-9]x){3}
3x3x3x 3x3x 4x4x4x 3x3x3x3x
Common Patterns
URL
(https?://)(www.)?(?<domain>[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-
z]{2,6})(?<path>/[-a-zA-Z0-9@:%_/+.~#?&=]*)?
CREDIT CARDS
'/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((
12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5])))
)[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9
]{11}|3[47][0-9]{13})*$/'
USERNAMES
/^[a-z0-9_-]{3,16}$/
PASSWORDS
/^[a-z0-9_-]{6,18}$/
IP ADDRESSES
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0
-4][0-9]|[01]?[0-9][0-9]?)$/
function isValidEmail (input) {
const regex = /^[^@s]+@[^@s]+.w{2,6}$/g;
const result = regex.exec(input)
return !!result
}
const tests = [
`test.test@gmail.com`, // Valid
`test.test`, // Invalid
'@invalid@test.com', // Invalid
`this is a test@test.com`, // Invalid
]
console.log(tests.map(isValidEmail))
Regex in Javascript Demo
<Codepen>
exec() returns an array with the matched text (or null) and any captured text
test() returns true/false if there is a match
match() returns result array (or null)
search() returns the index of the first match (or -1 if not found)
replace() returns new string where matches of a pattern are replaced
split() returns an array of substrings broken using the regex/string

More Related Content

PPTX
Regular Expression
PPTX
Regular Expressions 101 Introduction to Regular Expressions
ODP
Regex Presentation
KEY
Regular Expressions 101
PDF
Regular expression
PPTX
Regular expressions
PDF
Python Programming - X. Exception Handling and Assertions
PPTX
Regular expressions
Regular Expression
Regular Expressions 101 Introduction to Regular Expressions
Regex Presentation
Regular Expressions 101
Regular expression
Regular expressions
Python Programming - X. Exception Handling and Assertions
Regular expressions

What's hot (20)

PPTX
Input output statement in C
PPTX
Regular Expression (Regex) Fundamentals
PPT
Regular expressions
PDF
An Introduction to Regular expressions
PPT
Regular Expressions
PPTX
Regular Expression
PPT
Regular Expressions
PPTX
Relational algebra
PDF
Java Arrays
PPTX
Array in C
PPTX
Standard Template Library
PPTX
Recursive Function
PPTX
Variables, Data Types, Operator & Expression in c in detail
PPTX
joins in database
PPT
Stl Containers
PDF
Searching and Sorting Techniques in Data Structure
PPTX
Symbol Table
PPTX
Exploratory data analysis in R - Data Science Club
PPTX
Array and string
PPT
Normal-forms-for-Context-Free-Grammars.ppt
Input output statement in C
Regular Expression (Regex) Fundamentals
Regular expressions
An Introduction to Regular expressions
Regular Expressions
Regular Expression
Regular Expressions
Relational algebra
Java Arrays
Array in C
Standard Template Library
Recursive Function
Variables, Data Types, Operator & Expression in c in detail
joins in database
Stl Containers
Searching and Sorting Techniques in Data Structure
Symbol Table
Exploratory data analysis in R - Data Science Club
Array and string
Normal-forms-for-Context-Free-Grammars.ppt
Ad

Similar to Regex - Regular Expression Basics (20)

PPT
regex.ppt
PPTX
Regular Expression Crash Course
PDF
Basta mastering regex power
PPTX
Chapter 3: Introduction to Regular Expression
PDF
Regex startup
PDF
2013 - Andrei Zmievski: Clínica Regex
PDF
Regular expressions
PPT
Regular Expressions in PHP, MySQL by programmerblog.net
PPT
Regular Expressions grep and egrep
PPT
Bioinformatica 06-10-2011-p2 introduction
PPT
Regular Expressions 2007
PPTX
Bioinformatics p2-p3-perl-regexes v2014
PPTX
Regular expressions
PPTX
Regular Expressions Boot Camp
ODP
Regular Expressions and You
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
PPT
Perl Intro 5 Regex Matches And Substitutions
PPT
Chapter-three automata and complexity theory.ppt
ODP
OISF: Regular Expressions (Regex) Overview
regex.ppt
Regular Expression Crash Course
Basta mastering regex power
Chapter 3: Introduction to Regular Expression
Regex startup
2013 - Andrei Zmievski: Clínica Regex
Regular expressions
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions grep and egrep
Bioinformatica 06-10-2011-p2 introduction
Regular Expressions 2007
Bioinformatics p2-p3-perl-regexes v2014
Regular expressions
Regular Expressions Boot Camp
Regular Expressions and You
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Perl Intro 5 Regex Matches And Substitutions
Chapter-three automata and complexity theory.ppt
OISF: Regular Expressions (Regex) Overview
Ad

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
medical staffing services at VALiNTRY
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
AI in Product Development-omnex systems
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
ai tools demonstartion for schools and inter college
Operating system designcfffgfgggggggvggggggggg
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms II-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
Understanding Forklifts - TECH EHS Solution
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
CHAPTER 2 - PM Management and IT Context
Wondershare Filmora 15 Crack With Activation Key [2025
VVF-Customer-Presentation2025-Ver1.9.pptx
medical staffing services at VALiNTRY
Odoo Companies in India – Driving Business Transformation.pdf
AI in Product Development-omnex systems
How Creative Agencies Leverage Project Management Software.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
ManageIQ - Sprint 268 Review - Slide Deck
Internet Downloader Manager (IDM) Crack 6.42 Build 41
ai tools demonstartion for schools and inter college

Regex - Regular Expression Basics

  • 2. Pattern Matching Powerful and widely applicable technique used across various programming languages. Everything can be written using characters ⇒ use regex to search for a pattern of characters! Uses Cases: ● validate user input (emails, passwords) ● search text/code (vscode), replace/rename ● query databases ● extracting info from text (incl. web scraping) ● data massaging from raw ● File Renaming Web directives (Apache) ● interact with the Unix shell ● refactor code Why it can be intimidating:
  • 3. Regex Basics / open / close g after regex to make the search global (don’t stop!) i after regex to make the search case-insensitive m after regex to perform multiline matching /unicorn/ literal exact match escape special characters! ^.[$()|*+?{ tnr click me! 3/regex/gimflags
  • 4. . any character except newline d digit character D non-digit character w “word” character (alphanumeric or _) W non-word character s whitespace character (space, tab, newline, carriage return...) S not whitespace character Metacharacters
  • 5. Anchors — ^text$ ^ line start with (multiline flag) $ line end with /^The end$/gim ^The matches any string that starts with The end$ matches a string that ends with end ^The end$ starts and ends with The end Boundaries — b b word boundary B not a word
  • 6. Quantifiers — *, +, ? and {} * 0+ times (optional) + 1+ times (1 required) ? 0-1 times (optional) *lazy {#} # number of times {2,} 2+ times {2,5} 2-5 times 0(?=abc) match 0 only if followed by “abc” vs 0(?!abc) (?<=abc)0 match 0 only if preceded by “abc” vs (?<!abc)0 /hel{2}o/i; hello hello Helo helllo /hel{2,4}o/i; hello hellllo Helo helllo /hel{2,}o/i; (2+) hello helllllo helo helo
  • 7. [A-Z] uppercase characters (in range) a-z [a-z] lowercase characters (in range) a-z [A-Za-z] any letter [aeiou] either a, e, i, o or u [0-9] match any digits 0-9 (specific numbers [1-3] vs d) Character Sets — [] /gr[ae]y/i ⇒ Third character must be “a” or “e” [^aeiou] find any character not a, e, i, o or u [^2-4] match any digits not 2, 3, 4
  • 8. Grouping — () a(bc){2,5} Parens create a capturing group of “bc” ⇒ matches “a” followed by 2-5 “bc”s | or ⇒ (x|y)==[xy] both matches x or y (?:ab) match “ab” but don’t remember (“capture”) it (demo|example)[0-9]+ demo1 demoexample2 example4 demo 9 ([0-9]x){3} 3x3x3x 3x3x 4x4x4x 3x3x3x3x
  • 9. Common Patterns URL (https?://)(www.)?(?<domain>[-a-zA-Z0-9@:%._+~#=]{2,256}.[a- z]{2,6})(?<path>/[-a-zA-Z0-9@:%_/+.~#?&=]*)? CREDIT CARDS '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622(( 12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))) )[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9 ]{11}|3[47][0-9]{13})*$/' USERNAMES /^[a-z0-9_-]{3,16}$/ PASSWORDS /^[a-z0-9_-]{6,18}$/ IP ADDRESSES /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0 -4][0-9]|[01]?[0-9][0-9]?)$/ function isValidEmail (input) { const regex = /^[^@s]+@[^@s]+.w{2,6}$/g; const result = regex.exec(input) return !!result } const tests = [ `test.test@gmail.com`, // Valid `test.test`, // Invalid '@invalid@test.com', // Invalid `this is a test@test.com`, // Invalid ] console.log(tests.map(isValidEmail))
  • 10. Regex in Javascript Demo <Codepen> exec() returns an array with the matched text (or null) and any captured text test() returns true/false if there is a match match() returns result array (or null) search() returns the index of the first match (or -1 if not found) replace() returns new string where matches of a pattern are replaced split() returns an array of substrings broken using the regex/string