SlideShare a Scribd company logo
REGULAR EXPRESSIONSREGULAR EXPRESSIONS
WHAT IS REGULAR EXPRESSION?WHAT IS REGULAR EXPRESSION?
A regular expression, regex or regexp (sometimes
called a rational expression) is a sequence of characters
that de ne a search pattern.
Wikipedia
MDN
JS book
WHERE DO WE USE REGULAR EXPRESSIONS?WHERE DO WE USE REGULAR EXPRESSIONS?
Search engines
Data validation
Data scraping (especially web scraping)
Data wrangling
Parsing
PATTERNS AND FLAGSPATTERNS AND FLAGS
PATTERNSPATTERNS
The “long” syntax:
The “short” one, using slashes "/":
Inserting variables in string:
let regexp = new RegExp("pattern", "flags");1
let regexp = /pattern/; // no flags
let regexp = /pattern/g; // with flags g
1
2
let language = 'JavaScript';
let regexp = new RegExp(`${language} is the awesome!`);
1
2
FLAGSFLAGS
i (case-insensitive search)
g (the search looks for all matches)
y (“Sticky” mode)
u (full unicode support)
m (multiline mode)
s (“dotall” mode)
SEARCHING: STR.MATCH()SEARCHING: STR.MATCH()
1. If the regular expression has ag g, it returns an array
of all matches:
2. Otherwise returns only the rst match in the form of
an array with additional information:
3. If there are no matches, null is returned:
let res = 'We will we will'.match(/we/gi); // We we1
let res = 'We will we will'.match(/we/); // we groups index1
let res = 'JavaScript'.match(/HTML/); // null1
METACHARACTERSMETACHARACTERS
SETS AND RANGES [...]SETS AND RANGES [...]
A bracket expression. Matches a single character that is
contained within the brackets.
Square brackets may also contain character ranges.
Besides normal ranges, there are “excluding” ranges
that look like [^…]. const matches = 'Mop top
wop'.match(/[^tm]op/gi) // wop
let matches = 'Mop top'.match(/[tm]op/gi) // Mop top1
let matches = 'Zebra Zebrb Zebrz'.match(/Zebr[a-z]/gi) // Zebra1
CHARACTER CLASSESCHARACTER CLASSES
d (“d” is from “digit”) - same as [0-9]
s (“s” is from “space”) - same as [ trnvf]
w (“w” is from “word”) - same as [A-Za-z0-9_]
D - non-digits - same as [^0-9]
S – all but s - same as [^ trnvf]
W – all but w - same as [^A-Za-z0-9_]
"." - any character except a newline (any character
with 's' ag)
QUANTIFIERS +, *, ? AND {N}QUANTIFIERS +, *, ? AND {N}
The exact count: {n}
The range: {n, m}, match n-m times:
The range without upper limit: {n,}
let answer = "I'm 12345 years old".match(/d{5}/); // "12345"1
let answer = "I'm 1234 years old".match(/d{3,5}/); // "1234"1
let answer = "I'm 345678 years old".match(/d{3,}/); // "3456781
QUANTIFIERS +, *, ? AND {N}QUANTIFIERS +, *, ? AND {N}
+ “one or more” - same as {1,}
? “zero or one” - same as {0,1}
* “zero or more” - same as {0,}
let str = '100 10 1'.match(/d0+/g) ); // 100 101
let res = 'Colour or color?'.match(/colou?r/g); // color colour1
let str = '100 10 1'.match(/d0*/g); // 100, 10, 11
ANCHORS: STRING START ^ AND END $ANCHORS: STRING START ^ AND END $
Examples:
Multiline example:
let regexp = /^Hello.*/; // starts with Hello
let pattern = /.*Bye$/; // ends with Bye'
1
2
let str = `1st place: Winnie
2nd place: Piglet
3rd place: Eeyore`;
let result = str.match(/^d/gm); // 1, 2, 3
1
2
3
4
5
CAPTURING GROUPSCAPTURING GROUPS
Parentheses group characters together:
Parentheses group characters together:
'Gogogo now!'.match(/(go)+/i); // Gogogo1
let str = '<span class="my">';
let regexp = /<(([a-z]+)s*([^>]*))>/;
let result = str.match(regexp);
alert(result[0]); // <span class="my">
alert(result[1]); // span class="my"
alert(result[2]); // span
alert(result[3]); // class="my"</span></span>
1
2
3
4
5
6
7
8
ALTERNATION (OR) |ALTERNATION (OR) |
A usage example:
let res = 'Gray or grey?'.match(/gr(a|e)y/g); // Gray grey1
CONCLUSIONCONCLUSION
Problems:
Can't be used for recursive structures
Can be very slow solving complex logic
THANK YOU FORTHANK YOU FOR
ATTENTION!ATTENTION!

More Related Content

PDF
Scala presentatie
gedejong
 
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
PDF
Regular expressions
keeyre
 
PDF
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
PPTX
Regular expression unit2
smitha273566
 
PDF
Regular expressions
Raghu nath
 
PPT
Regular expressions
Raj Gupta
 
PPT
regular-expressions lecture 28-string regular expression
smallboss311
 
Scala presentatie
gedejong
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
Regular expressions
keeyre
 
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
Regular expression unit2
smitha273566
 
Regular expressions
Raghu nath
 
Regular expressions
Raj Gupta
 
regular-expressions lecture 28-string regular expression
smallboss311
 

Similar to Regular expressions (20)

KEY
Regular Expressions 101
Raj Rajandran
 
PPTX
Regular Expressions
Akhil Kaushik
 
PPT
Chapter-three automata and complexity theory.ppt
anwarkade1
 
PDF
Construction of a predictive parsing table.pdf
anwarkade1
 
PDF
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
PPT
Regular Expression in Action
Folio3 Software
 
PDF
An Introduction to Regular expressions
Yamagata Europe
 
PPTX
Mikhail Khristophorov "Introduction to Regular Expressions"
LogeekNightUkraine
 
PPTX
Regular expressions
Ивелин Кирилов
 
PDF
Practical JavaScript Programming - Session 6/8
Wilson Su
 
PPT
Regular Expressions
Satya Narayana
 
PPTX
Regular Expression Crash Course
Imran Qasim
 
PDF
Regex startup
PayPal
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
JSregularExpressions.pptx
MattMarino13
 
PDF
3.2 javascript regex
Jalpesh Vasa
 
PPTX
REGULAR EXPRESSION FOR NATURAL LANGUAGES
ssuser77162c
 
PPTX
Regular expressions
Thomas Langston
 
PPT
16 Java Regex
wayn
 
Regular Expressions 101
Raj Rajandran
 
Regular Expressions
Akhil Kaushik
 
Chapter-three automata and complexity theory.ppt
anwarkade1
 
Construction of a predictive parsing table.pdf
anwarkade1
 
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Regular Expression in Action
Folio3 Software
 
An Introduction to Regular expressions
Yamagata Europe
 
Mikhail Khristophorov "Introduction to Regular Expressions"
LogeekNightUkraine
 
Regular expressions
Ивелин Кирилов
 
Practical JavaScript Programming - Session 6/8
Wilson Su
 
Regular Expressions
Satya Narayana
 
Regular Expression Crash Course
Imran Qasim
 
Regex startup
PayPal
 
Regular expressions in Python
Sujith Kumar
 
JSregularExpressions.pptx
MattMarino13
 
3.2 javascript regex
Jalpesh Vasa
 
REGULAR EXPRESSION FOR NATURAL LANGUAGES
ssuser77162c
 
Regular expressions
Thomas Langston
 
16 Java Regex
wayn
 
Ad

Recently uploaded (20)

PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Doc9.....................................
SofiaCollazos
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Ad

Regular expressions

  • 2. WHAT IS REGULAR EXPRESSION?WHAT IS REGULAR EXPRESSION? A regular expression, regex or regexp (sometimes called a rational expression) is a sequence of characters that de ne a search pattern. Wikipedia MDN JS book
  • 3. WHERE DO WE USE REGULAR EXPRESSIONS?WHERE DO WE USE REGULAR EXPRESSIONS? Search engines Data validation Data scraping (especially web scraping) Data wrangling Parsing
  • 5. PATTERNSPATTERNS The “long” syntax: The “short” one, using slashes "/": Inserting variables in string: let regexp = new RegExp("pattern", "flags");1 let regexp = /pattern/; // no flags let regexp = /pattern/g; // with flags g 1 2 let language = 'JavaScript'; let regexp = new RegExp(`${language} is the awesome!`); 1 2
  • 6. FLAGSFLAGS i (case-insensitive search) g (the search looks for all matches) y (“Sticky” mode) u (full unicode support) m (multiline mode) s (“dotall” mode)
  • 7. SEARCHING: STR.MATCH()SEARCHING: STR.MATCH() 1. If the regular expression has ag g, it returns an array of all matches: 2. Otherwise returns only the rst match in the form of an array with additional information: 3. If there are no matches, null is returned: let res = 'We will we will'.match(/we/gi); // We we1 let res = 'We will we will'.match(/we/); // we groups index1 let res = 'JavaScript'.match(/HTML/); // null1
  • 9. SETS AND RANGES [...]SETS AND RANGES [...] A bracket expression. Matches a single character that is contained within the brackets. Square brackets may also contain character ranges. Besides normal ranges, there are “excluding” ranges that look like [^…]. const matches = 'Mop top wop'.match(/[^tm]op/gi) // wop let matches = 'Mop top'.match(/[tm]op/gi) // Mop top1 let matches = 'Zebra Zebrb Zebrz'.match(/Zebr[a-z]/gi) // Zebra1
  • 10. CHARACTER CLASSESCHARACTER CLASSES d (“d” is from “digit”) - same as [0-9] s (“s” is from “space”) - same as [ trnvf] w (“w” is from “word”) - same as [A-Za-z0-9_] D - non-digits - same as [^0-9] S – all but s - same as [^ trnvf] W – all but w - same as [^A-Za-z0-9_] "." - any character except a newline (any character with 's' ag)
  • 11. QUANTIFIERS +, *, ? AND {N}QUANTIFIERS +, *, ? AND {N} The exact count: {n} The range: {n, m}, match n-m times: The range without upper limit: {n,} let answer = "I'm 12345 years old".match(/d{5}/); // "12345"1 let answer = "I'm 1234 years old".match(/d{3,5}/); // "1234"1 let answer = "I'm 345678 years old".match(/d{3,}/); // "3456781
  • 12. QUANTIFIERS +, *, ? AND {N}QUANTIFIERS +, *, ? AND {N} + “one or more” - same as {1,} ? “zero or one” - same as {0,1} * “zero or more” - same as {0,} let str = '100 10 1'.match(/d0+/g) ); // 100 101 let res = 'Colour or color?'.match(/colou?r/g); // color colour1 let str = '100 10 1'.match(/d0*/g); // 100, 10, 11
  • 13. ANCHORS: STRING START ^ AND END $ANCHORS: STRING START ^ AND END $ Examples: Multiline example: let regexp = /^Hello.*/; // starts with Hello let pattern = /.*Bye$/; // ends with Bye' 1 2 let str = `1st place: Winnie 2nd place: Piglet 3rd place: Eeyore`; let result = str.match(/^d/gm); // 1, 2, 3 1 2 3 4 5
  • 14. CAPTURING GROUPSCAPTURING GROUPS Parentheses group characters together: Parentheses group characters together: 'Gogogo now!'.match(/(go)+/i); // Gogogo1 let str = '<span class="my">'; let regexp = /<(([a-z]+)s*([^>]*))>/; let result = str.match(regexp); alert(result[0]); // <span class="my"> alert(result[1]); // span class="my" alert(result[2]); // span alert(result[3]); // class="my"</span></span> 1 2 3 4 5 6 7 8
  • 15. ALTERNATION (OR) |ALTERNATION (OR) | A usage example: let res = 'Gray or grey?'.match(/gr(a|e)y/g); // Gray grey1
  • 16. CONCLUSIONCONCLUSION Problems: Can't be used for recursive structures Can be very slow solving complex logic
  • 17. THANK YOU FORTHANK YOU FOR ATTENTION!ATTENTION!