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

More Related Content

What's hot

Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sortingAjharul Abedeen
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL ISankhya_Analytics
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational CalculusKunal Anand
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in databaseSatya P. Joshi
 
DBMS _Relational model
DBMS _Relational modelDBMS _Relational model
DBMS _Relational modelAzizul Mamun
 
Data Match Merging in SAS
Data Match Merging in SASData Match Merging in SAS
Data Match Merging in SASguest2160992
 
MySQL 5.7 String Functions
MySQL 5.7 String FunctionsMySQL 5.7 String Functions
MySQL 5.7 String FunctionsFrancesco Marino
 
PHP POSTGRESQL integration
PHP POSTGRESQL  integrationPHP POSTGRESQL  integration
PHP POSTGRESQL integrationSandhya Sharma
 
Infobright Column-Oriented Analytical Database Engine
Infobright Column-Oriented Analytical Database EngineInfobright Column-Oriented Analytical Database Engine
Infobright Column-Oriented Analytical Database EngineAlex Esterkin
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Key and its different types
Key and its different typesKey and its different types
Key and its different typesUmair Shakir
 
Query optimization
Query optimizationQuery optimization
Query optimizationNeha Behl
 
AI Propositional logic
AI Propositional logicAI Propositional logic
AI Propositional logicSURBHI SAROHA
 
REGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.AREGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.ADev Ashish
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners PerlDave Cross
 
SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 

What's hot (20)

Functional dependency
Functional dependencyFunctional dependency
Functional dependency
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational Calculus
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
DBMS _Relational model
DBMS _Relational modelDBMS _Relational model
DBMS _Relational model
 
Data Match Merging in SAS
Data Match Merging in SASData Match Merging in SAS
Data Match Merging in SAS
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
MySQL 5.7 String Functions
MySQL 5.7 String FunctionsMySQL 5.7 String Functions
MySQL 5.7 String Functions
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
PHP POSTGRESQL integration
PHP POSTGRESQL  integrationPHP POSTGRESQL  integration
PHP POSTGRESQL integration
 
Infobright Column-Oriented Analytical Database Engine
Infobright Column-Oriented Analytical Database EngineInfobright Column-Oriented Analytical Database Engine
Infobright Column-Oriented Analytical Database Engine
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Key and its different types
Key and its different typesKey and its different types
Key and its different types
 
Query optimization
Query optimizationQuery optimization
Query optimization
 
AI Propositional logic
AI Propositional logicAI Propositional logic
AI Propositional logic
 
REGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.AREGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.A
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 

Similar to 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
 

Similar to 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
 

Recently uploaded

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
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
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
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
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
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
 
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.
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
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
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 

Recently uploaded (20)

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
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
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
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
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
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
 
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 ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
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...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 

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