SlideShare a Scribd company logo
1 of 17
Download to read offline
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

Similar to Regular expressions

Coffee 'n code: Regexes
Coffee 'n code: RegexesCoffee 'n code: Regexes
Coffee 'n code: RegexesPhil Ewels
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Lecture 23
Lecture 23Lecture 23
Lecture 23rhshriva
 
Eag 201110-hrugregexpresentation-111006104128-phpapp02
Eag 201110-hrugregexpresentation-111006104128-phpapp02Eag 201110-hrugregexpresentation-111006104128-phpapp02
Eag 201110-hrugregexpresentation-111006104128-phpapp02egoodwintx
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeProf. Wim Van Criekinge
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionKuyseng Chhoeun
 
Form validation client side
Form validation client side Form validation client side
Form validation client side Mudasir Syed
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Regular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And BeyondRegular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And BeyondMax Shirshin
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionskeeyre
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and StatementsWebStackAcademy
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionLambert Lum
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 

Similar to Regular expressions (20)

Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Coffee 'n code: Regexes
Coffee 'n code: RegexesCoffee 'n code: Regexes
Coffee 'n code: Regexes
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Eag 201110-hrugregexpresentation-111006104128-phpapp02
Eag 201110-hrugregexpresentation-111006104128-phpapp02Eag 201110-hrugregexpresentation-111006104128-phpapp02
Eag 201110-hrugregexpresentation-111006104128-phpapp02
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
 
Form validation client side
Form validation client side Form validation client side
Form validation client side
 
Javascript2839
Javascript2839Javascript2839
Javascript2839
 
Json
JsonJson
Json
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Regular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And BeyondRegular Expressions: JavaScript And Beyond
Regular Expressions: JavaScript And Beyond
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 

Recently uploaded

Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptxFIDO Alliance
 
“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
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
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
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
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
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfOverkill Security
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe中 央社
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهMohamed Sweelam
 
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
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxFIDO Alliance
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.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
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
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
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?Paolo Missier
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctBrainSell Technologies
 

Recently uploaded (20)

Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
“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
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
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
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
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
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdf
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهله
 
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
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.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
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
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
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 

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!