SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
FUNDAMENTALS OF REGULAR
EXPRESSION (RegEX)
With demonstration using JavaScript
By : Azzter
Weekly Discord Knowledge-sharing
What is Regular Expression or RegEx?
-A Regular Expression (RegEx) is a sequence of
characters that defines a search pattern.
-It describes a pattern of text
• can test whether a string matches the expr's
pattern
• can use a regex to search/replace characters in
a string
• very powerful, but tough to read
Blue highlights show the match results of the regular expression pattern /h[aeiou]+/g
(the letter h followed by one or more vowels)
Application
Usually, such patterns are used by string-searching
algorithms for "find" or "find and replace" operations on
strings, or for input validation.
Regexes are useful in a wide variety of text
processing tasks, and more generally string
processing, where the data need not be textual.
Common applications include data validation,
data scraping (especially web scraping), data
wrangling, simple parsing, the production of
syntax highlighting systems, and many other
tasks.
Programming Languages that supports RegEx
Most general-purpose programming languages support regex capabilities, either natively or via
libraries. Comprehensivesupport is included in:
• C
• C++
• Java
• JavaScript
• Perl
• PHP
• Python
• Rust
Basic Syntax: Delimiter
•All RegEx statements must begin and end with / . This is called delimiter
•/someString/
Example: confirm if the string contains the word “dog”
STRING: “The quick brown fox jumps over the lazy dog.”
PATTERN: /dog/
Note: In Python, regular expressions do not require delimiters to separate the regular expression pattern from the surrounding text.
Modifiers/Flags are used to
perform case-insensitive and
global searches
/someString/g
/someString/i
/someStringn
anotherStringLine /m
Basic Syntax: Modifiers/Flags
Modifier Description
g Perform a global match (find all matches
rather than stopping after the first match)
i Perform case-insensitive matching
m Perform multiline matching
Example: confirm if the string contains multiple word for “DoG”
STRING: “The quick brown dog jumps over the lazy dog.”
PATTERN: /dog/gi flags can be
combined:
Basic Syntax: Boolean OR
Example: confirm if the string contains word for “dog” or “cat”
STRING: “The quick brown fox jumps over the lazy cat.”
PATTERN: /dog|cat/
-Find any of the alternatives specified
| means OR
"abc|def|g" matches lines with "abc", "def", or "g"
There's no AND symbol.
Basic Syntax: Parenthesis
() are for grouping
/(Homer|Marge) Simpson/ matches lines containing "Homer Simpson" or "Marge Simpson"
let text = "01234567890123456789";
let pattern = /(0|5|7)/g;
Do a global search to find any of the specified
alternatives (0|5|7):
Brackets are used
to find a range of
characters and
either inside a
character sets
Basic Syntax: Brackets
Expression Description
[abc] Find any character between the brackets (character set)
[0-9] Find any character between the brackets (any digit)
(character range)
[A-Z] Find any character between the brackets (any uppercase
alphabet character) (character range)
[0-9a-z] Find any character between the brackets (any
alphanumeric character) (character range)
"[bcd]art" matches strings containing "bart", "cart", and "dart"
equivalent to "(b|c|d)art" but shorter
inside [ ], most modifier keys act as normal characters
"what[.!*?]*" matches "what", "what.", "what!", "what?**!"
Modifier keys like . ! * and ? Is discussed
in next few slides
Basic Syntax: Brackets
an initial ^ inside a character set negates it
"[^abcd]" matches any character other than a, b, c, or d
inside a character set, - must be escaped to be matched
"[+-.]?[0-9]+" matches optional +, . or -, followed by  one digit
Basic Syntax: Escape sequence 
• many characters must be escaped to match them: /  $ . [ ] ( ) ^ * + ?
• ".n" matches lines containing ".n"
Bypass metacharacter or special characters as literal character:
Example:
• (
• )
• ?
• .
• etc…
Basic Syntax: Built-in character ranges
b Find a match at the beginning/end of a word, beginning like this: bHI, end like this: HIb
B Find a match, but not at the beginning/end of a word
d any digit; equivalent to [0-9]
D any non-digit; equivalent to [^0-9]
s any whitespace character; [ fnrtv...]
s any non-whitespace character
w any word character; [A-Za-z0-9_]
W any non-word character
Basic Syntax: Quantifiers
• * means 0 or more occurrences
"abc*" matches "ab", "abc", "abcc", "abccc", ...
"a(bc)*" matches "a", "abc", "abcbc", "abcbcbc", ...
"a.*a" matches "aa", "aba", "a8qa", "a!?_a", ...
• + means 1 or more occurrences
"a(bc)+" matches "abc", "abcbc", "abcbcbc", ...
"Goo+gle" matches "Google", "Gooogle", "Goooogle", ...
• ? means 0 or 1 occurrences
"Martina?" matches lines with "Martin" or "Martina"
"Dan(iel)?" matches lines with "Dan" or "Daniel“
Basic Syntax: Quantifiers
• ^ Matches the beginning of input. If the multiline flag is set to true, also matches
immediately after a line break character. For example, /^A/ does not match the "A" in
"an A", but does match the first "A" in "An A".
• x(?=n) A positive lookahead is a construct in regular expressions that allows you to
match a group of characters only if they are followed by another specific pattern.
Positive lookaheads are written using the syntax (?=pattern).
• x(?!y) Negative lookahead assertion: Matches "x" only if "x" is not followed by "y".
For example, /d+(?!.)/ matches a number only if it is not followed by a decimal
point. /d+(?!.)/.exec('3.141') matches "141" but not "3".
Can positive lookahead first argument be empty?
Yes, a positive lookahead can have an empty first argument.
When the first argument of a positive lookahead is empty, it matches any position in the string that is followed by the pattern specified in the lookahead. This can
be useful in cases where you want to ensure that a certain pattern occurs somewhere in the string, but you don't want to match that pattern.
Basic Syntax: Quantifiers
• {min,max} means between min and max occurrences
"a(bc){2,4}" matches "abcbc", "abcbcbc", or "abcbcbcbc"
• min or max may be omitted to specify any number
"{2,}" means 2 or more
"{,6}" means up to 6
"{3}" means exactly 3
JavaScript RegEx methods
exec() :tests for a match in a string.
If it finds a match, it returns a result array, otherwise it returns null.
test() :tests for a match in a string.
If it finds a match, it returns true, otherwise it returns false.
toString(): returns the string value of the regular expression.
Example: email validator:
const emailRegex = /^[^s@]+@[^s@]+.[^s@]+$/;
function validateEmail(email) {
return emailRegex.test(email);
}
Example: phone number validator in the format (123) 456-7890:
const phoneRegex = /^(d{3}) d{3}-d{4}$/;
function validatePhoneNumber(phoneNumber) {
return phoneRegex.test(phoneNumber);
}
Example: Validate a URL that starts with https:// or http://:
const urlRegex = /^https?://[w-]+(.[w-
]+)+[/#?]?.*$/;
function validateUrl(url) {
return urlRegex.test(url);
}
Example: Remove all non-alphanumeric characters from a string:
const str = "Hello, world!";
const alphanumericStr = str.replace(/[^a-zA-Z0-9]/g, '');
console.log(alphanumericStr); // Output: "Helloworld"
Example: Extract all email addresses from a string:
const emailRegex = /[^s@]+@[^s@]+.[^s@]+/g;
const str = "Contact us at info@example.com or
sales@example.com for more information.";
const emailList = str.match(emailRegex);
console.log(emailList); // Output: ["info@example.com",
"sales@example.com"]
Example: Validate a password that contains at least one uppercase letter, one
lowercase letter, and one digit, and is at least 8 characters long:
const passwordRegex = /^(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
function validatePassword(password) {
return passwordRegex.test(password);
}
console.log(validatePassword("Password123")); // Output: true
console.log(validatePassword("password")); // Output: false

Weitere ähnliche Inhalte

Was ist angesagt?

Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101Raj Rajandran
 
Web api crud operations
Web api crud operationsWeb api crud operations
Web api crud operationsEyal Vardi
 
How to Automate API Testing
How to Automate API TestingHow to Automate API Testing
How to Automate API TestingBruno Pedro
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test frameworkAbner Chih Yi Huang
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackGaryCoady
 
SAML Protocol Overview
SAML Protocol OverviewSAML Protocol Overview
SAML Protocol OverviewMike Schwartz
 
Locking down your Kubernetes cluster with Linkerd
Locking down your Kubernetes cluster with LinkerdLocking down your Kubernetes cluster with Linkerd
Locking down your Kubernetes cluster with LinkerdBuoyant
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with JestMichał Pierzchała
 
GO programming language
GO programming languageGO programming language
GO programming languagetung vu
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injectionEyal Vardi
 
대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐
대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐
대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐Terry Cho
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest MatchersShai Yallin
 
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google MockICS
 
Pentesting Rest API's by :- Gaurang Bhatnagar
Pentesting Rest API's by :- Gaurang BhatnagarPentesting Rest API's by :- Gaurang Bhatnagar
Pentesting Rest API's by :- Gaurang BhatnagarOWASP Delhi
 
IBM Datapower Security Scenario with JWS & JWE
IBM Datapower Security Scenario with JWS & JWEIBM Datapower Security Scenario with JWS & JWE
IBM Datapower Security Scenario with JWS & JWEsandipg123
 

Was ist angesagt? (20)

Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
 
Web api crud operations
Web api crud operationsWeb api crud operations
Web api crud operations
 
How to Automate API Testing
How to Automate API TestingHow to Automate API Testing
How to Automate API Testing
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web Stack
 
SAML Protocol Overview
SAML Protocol OverviewSAML Protocol Overview
SAML Protocol Overview
 
Locking down your Kubernetes cluster with Linkerd
Locking down your Kubernetes cluster with LinkerdLocking down your Kubernetes cluster with Linkerd
Locking down your Kubernetes cluster with Linkerd
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
Trees and Hierarchies in SQL
Trees and Hierarchies in SQLTrees and Hierarchies in SQL
Trees and Hierarchies in SQL
 
GO programming language
GO programming languageGO programming language
GO programming language
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐
대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐
대용량 분산 아키텍쳐 설계 #3 대용량 분산 시스템 아키텍쳐
 
Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest Matchers
 
CMake best practices
CMake best practicesCMake best practices
CMake best practices
 
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
 
Pentesting Rest API's by :- Gaurang Bhatnagar
Pentesting Rest API's by :- Gaurang BhatnagarPentesting Rest API's by :- Gaurang Bhatnagar
Pentesting Rest API's by :- Gaurang Bhatnagar
 
IBM Datapower Security Scenario with JWS & JWE
IBM Datapower Security Scenario with JWS & JWEIBM Datapower Security Scenario with JWS & JWE
IBM Datapower Security Scenario with JWS & JWE
 

Ähnlich wie FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf

Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Sandy Smith
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007Geoffrey Dunn
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 TrainingChris Chubb
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)Chirag Shetty
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat SheetSydneyJohnson57
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in pythonJohn(Qiang) Zhang
 

Ähnlich wie FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf (20)

Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
 
Regex posix
Regex posixRegex posix
Regex posix
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
Regex Basics
Regex BasicsRegex Basics
Regex Basics
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat Sheet
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 

Kürzlich hochgeladen

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Kürzlich hochgeladen (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 

FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf

  • 1. FUNDAMENTALS OF REGULAR EXPRESSION (RegEX) With demonstration using JavaScript By : Azzter Weekly Discord Knowledge-sharing
  • 2. What is Regular Expression or RegEx? -A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. -It describes a pattern of text • can test whether a string matches the expr's pattern • can use a regex to search/replace characters in a string • very powerful, but tough to read Blue highlights show the match results of the regular expression pattern /h[aeiou]+/g (the letter h followed by one or more vowels)
  • 3. Application Usually, such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. Regexes are useful in a wide variety of text processing tasks, and more generally string processing, where the data need not be textual. Common applications include data validation, data scraping (especially web scraping), data wrangling, simple parsing, the production of syntax highlighting systems, and many other tasks.
  • 4. Programming Languages that supports RegEx Most general-purpose programming languages support regex capabilities, either natively or via libraries. Comprehensivesupport is included in: • C • C++ • Java • JavaScript • Perl • PHP • Python • Rust
  • 5. Basic Syntax: Delimiter •All RegEx statements must begin and end with / . This is called delimiter •/someString/ Example: confirm if the string contains the word “dog” STRING: “The quick brown fox jumps over the lazy dog.” PATTERN: /dog/ Note: In Python, regular expressions do not require delimiters to separate the regular expression pattern from the surrounding text.
  • 6. Modifiers/Flags are used to perform case-insensitive and global searches /someString/g /someString/i /someStringn anotherStringLine /m Basic Syntax: Modifiers/Flags Modifier Description g Perform a global match (find all matches rather than stopping after the first match) i Perform case-insensitive matching m Perform multiline matching Example: confirm if the string contains multiple word for “DoG” STRING: “The quick brown dog jumps over the lazy dog.” PATTERN: /dog/gi flags can be combined:
  • 7. Basic Syntax: Boolean OR Example: confirm if the string contains word for “dog” or “cat” STRING: “The quick brown fox jumps over the lazy cat.” PATTERN: /dog|cat/ -Find any of the alternatives specified | means OR "abc|def|g" matches lines with "abc", "def", or "g" There's no AND symbol.
  • 8. Basic Syntax: Parenthesis () are for grouping /(Homer|Marge) Simpson/ matches lines containing "Homer Simpson" or "Marge Simpson" let text = "01234567890123456789"; let pattern = /(0|5|7)/g; Do a global search to find any of the specified alternatives (0|5|7):
  • 9. Brackets are used to find a range of characters and either inside a character sets Basic Syntax: Brackets Expression Description [abc] Find any character between the brackets (character set) [0-9] Find any character between the brackets (any digit) (character range) [A-Z] Find any character between the brackets (any uppercase alphabet character) (character range) [0-9a-z] Find any character between the brackets (any alphanumeric character) (character range) "[bcd]art" matches strings containing "bart", "cart", and "dart" equivalent to "(b|c|d)art" but shorter inside [ ], most modifier keys act as normal characters "what[.!*?]*" matches "what", "what.", "what!", "what?**!" Modifier keys like . ! * and ? Is discussed in next few slides
  • 10. Basic Syntax: Brackets an initial ^ inside a character set negates it "[^abcd]" matches any character other than a, b, c, or d inside a character set, - must be escaped to be matched "[+-.]?[0-9]+" matches optional +, . or -, followed by  one digit
  • 11. Basic Syntax: Escape sequence • many characters must be escaped to match them: / $ . [ ] ( ) ^ * + ? • ".n" matches lines containing ".n" Bypass metacharacter or special characters as literal character: Example: • ( • ) • ? • . • etc…
  • 12. Basic Syntax: Built-in character ranges b Find a match at the beginning/end of a word, beginning like this: bHI, end like this: HIb B Find a match, but not at the beginning/end of a word d any digit; equivalent to [0-9] D any non-digit; equivalent to [^0-9] s any whitespace character; [ fnrtv...] s any non-whitespace character w any word character; [A-Za-z0-9_] W any non-word character
  • 13. Basic Syntax: Quantifiers • * means 0 or more occurrences "abc*" matches "ab", "abc", "abcc", "abccc", ... "a(bc)*" matches "a", "abc", "abcbc", "abcbcbc", ... "a.*a" matches "aa", "aba", "a8qa", "a!?_a", ... • + means 1 or more occurrences "a(bc)+" matches "abc", "abcbc", "abcbcbc", ... "Goo+gle" matches "Google", "Gooogle", "Goooogle", ... • ? means 0 or 1 occurrences "Martina?" matches lines with "Martin" or "Martina" "Dan(iel)?" matches lines with "Dan" or "Daniel“
  • 14. Basic Syntax: Quantifiers • ^ Matches the beginning of input. If the multiline flag is set to true, also matches immediately after a line break character. For example, /^A/ does not match the "A" in "an A", but does match the first "A" in "An A". • x(?=n) A positive lookahead is a construct in regular expressions that allows you to match a group of characters only if they are followed by another specific pattern. Positive lookaheads are written using the syntax (?=pattern). • x(?!y) Negative lookahead assertion: Matches "x" only if "x" is not followed by "y". For example, /d+(?!.)/ matches a number only if it is not followed by a decimal point. /d+(?!.)/.exec('3.141') matches "141" but not "3". Can positive lookahead first argument be empty? Yes, a positive lookahead can have an empty first argument. When the first argument of a positive lookahead is empty, it matches any position in the string that is followed by the pattern specified in the lookahead. This can be useful in cases where you want to ensure that a certain pattern occurs somewhere in the string, but you don't want to match that pattern.
  • 15. Basic Syntax: Quantifiers • {min,max} means between min and max occurrences "a(bc){2,4}" matches "abcbc", "abcbcbc", or "abcbcbcbc" • min or max may be omitted to specify any number "{2,}" means 2 or more "{,6}" means up to 6 "{3}" means exactly 3
  • 16. JavaScript RegEx methods exec() :tests for a match in a string. If it finds a match, it returns a result array, otherwise it returns null. test() :tests for a match in a string. If it finds a match, it returns true, otherwise it returns false. toString(): returns the string value of the regular expression.
  • 17. Example: email validator: const emailRegex = /^[^s@]+@[^s@]+.[^s@]+$/; function validateEmail(email) { return emailRegex.test(email); }
  • 18. Example: phone number validator in the format (123) 456-7890: const phoneRegex = /^(d{3}) d{3}-d{4}$/; function validatePhoneNumber(phoneNumber) { return phoneRegex.test(phoneNumber); }
  • 19. Example: Validate a URL that starts with https:// or http://: const urlRegex = /^https?://[w-]+(.[w- ]+)+[/#?]?.*$/; function validateUrl(url) { return urlRegex.test(url); }
  • 20. Example: Remove all non-alphanumeric characters from a string: const str = "Hello, world!"; const alphanumericStr = str.replace(/[^a-zA-Z0-9]/g, ''); console.log(alphanumericStr); // Output: "Helloworld"
  • 21. Example: Extract all email addresses from a string: const emailRegex = /[^s@]+@[^s@]+.[^s@]+/g; const str = "Contact us at info@example.com or sales@example.com for more information."; const emailList = str.match(emailRegex); console.log(emailList); // Output: ["info@example.com", "sales@example.com"]
  • 22. Example: Validate a password that contains at least one uppercase letter, one lowercase letter, and one digit, and is at least 8 characters long: const passwordRegex = /^(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/; function validatePassword(password) { return passwordRegex.test(password); } console.log(validatePassword("Password123")); // Output: true console.log(validatePassword("password")); // Output: false