SlideShare a Scribd company logo
Regular Expressions From Beginner To Expert By Varun
Date: December 24, 2018
In this document want to show how to master regex. I wasn’t good at regex until I realized its power and
the necessity to understand and master it.
---- Avoid writing lines and lines of code by using power of regex.
---- The regex shown here will take you from a simple example to the complex patterns.
---- Lot of tools apart from all the programming languages support regex.
---- I personally know integration tools such as Ca-Api-Gateway, Apigee, TibcoFlogo, Mulesoft where
regex can be used and match complex patterns, save unnecessary code for pattern match.
---- Logging slutions like ELK, Splunk support full on regex.
---- Analytical tools such as Tableau support full on regex.
---- Please follow ths document and practice along by the end of it. You will start writing complex regex
on your own.
Regexp By Varun Kumar 3134330169
Page 1 of 50
Contents
Introduction ......................................................................................................................................3
What are regular expressions.....................................................................................................3
Why to learn regular expressions ..............................................................................................3
How regex is created ...................................................................................................................3
Tools Used....................................................................................................................................4
Basic Syntax Regex .....................................................................................................................5
All About Characters .......................................................................................................................5
Character Literals.........................................................................................................................5
Character Classes........................................................................................................................6
Forward Boundary .......................................................................................................................7
Character Range...........................................................................................................................8
Regex Live Example ..................................................................................................................10
Negation Characters ..................................................................................................................12
Meta Characters .............................................................................................................................13
What are Meta Characters .........................................................................................................13
Wild Card Meta Characters Part 1 ............................................................................................15
Escaping Meta Characters ........................................................................................................19
Predifined Character Classes ...................................................................................................20
Anchors And Word Boundary.......................................................................................................22
Anchors.......................................................................................................................................22
Word Boundary ..........................................................................................................................24
Quantifiers ......................................................................................................................................26
? quantifier..................................................................................................................................26
* Quantifier..................................................................................................................................28
Matching Example......................................................................................................................28
+ Quantifier .................................................................................................................................32
{} Quantifiers To Limit Range ...................................................................................................33
{min,} Quantifiers .......................................................................................................................33
{min,max} Quantifiers................................................................................................................34
Greedy Quantifiers.....................................................................................................................35
Regexp By Varun Kumar 3134330169
Page 2 of 50
Lazy Or Reluctant Quantifiers...................................................................................................36
Alternative For Lazy Quantifiers...........................................................................................36
Greedy Quantifiers vs Lazy Quantifiers...............................................................................38
Increasing the performance of the RegeX...........................................................................39
Groups In Regex ............................................................................................................................40
Group Examples.........................................................................................................................40
Back Refrencing In Groups.......................................................................................................42
Non-Capturing Groups ..............................................................................................................43
Alteration In Groups ..................................................................................................................43
Nesting in Groups/Alternation ..................................................................................................44
Assertions.......................................................................................................................................45
Look Ahead Assertion ...............................................................................................................45
Positive Look Ahead Assertion ................................................................................................45
Negative Look Ahead Assertion ...............................................................................................46
Look Behind Assertions............................................................................................................46
Positive Look Behind Assertion...............................................................................................46
Negative Look Behind Assertion..............................................................................................47
Real Life Scenarios ........................................................................................................................47
Example From My Project Work ...................................................................................................49
Regexp By Varun Kumar 3134330169
Page 3 of 50
Introduction
What are regular expressions
Why to learn regular expressions
Used in all programming languages. Also used in caApiGateway and Apigee
For example validating user input into a form
How regex is created
Regexp By Varun Kumar 3134330169
Page 4 of 50
Tools Used
Tools used for learning the regex during the course of this document.
Online regex tester
https://regex101.com
https://regexr.com
https://www/regextester.com
Offline Regex Testers
https://atom.io
reach regex by using ctrl+F or below steps
Regexp By Varun Kumar 3134330169
Page 5 of 50
Regexp are case sensitive.
Basic Syntax Regex
Used in all programming languages. Also used in Api Management tools such as, caApiGateway and Apigee
All About Characters
Character Literals
Takes the string as it is.
Regexp By Varun Kumar 3134330169
Page 6 of 50
Regexby default is case sensitive.
:/Java/gm (Case sensitive)
:/Java/gmi (Case insensitive)
Regex starts searching from left to right
Character Classes
Character class are enclosed in [] brackets
Regexp By Varun Kumar 3134330169
Page 7 of 50
Forward Boundary
>> Forward boundary uses the following pattern ‘b’
say in this you want to match only the word bet and not sub string that may contain bet.
Regexp By Varun Kumar 3134330169
Page 8 of 50
Character Range
Regexp By Varun Kumar 3134330169
Page 9 of 50
Say there are 5 employes and you want to match only the emp 1,2,3
Write a regex to match the first 3 words.
Indexes of alphabets and numbers
Regexp By Varun Kumar 3134330169
Page 10 of 50
You needs to match the index when defining the character ranges you need to go from small to big
[A-z] This is correct
[a-Z] This is wrong.
Regex Live Example
<!DOCTYPE html>
<html>
<head>
<title>Testing Regular Expressions in HTML forms</title>
</head>
<body>
<h1>Test your Regular Expressions here</h1>
<fieldset>
<legend>Write your Regular expressions</legend>
<form>
<div>
<label for="display-name"> Prove that you are not a robot:
<span class="warning">*(Enter a single English capital letter.)</span>
</label>
<input type="text" id="display-name" name="ip-display"
pattern="[A-Z]"
title="Enter any single English capital Letter only"/>
</div>
<div>
<input type="submit" class="submit" value="Submit" />
</div>
</form>
</fieldset>
</body>
</html>
Regexp By Varun Kumar 3134330169
Page 11 of 50
Regexp By Varun Kumar 3134330169
Page 12 of 50
Negation Characters
Are represented with a ^ symbol inside [] brackets.
[^a-e] will match all characters except a-e
Example 1:-
 The First Character should be any character except a comma(,)
 The Second Character must be a comma(,)
 The Third Character should be any character except a comma(,)
Example 2:-
 The First Character must be a vowel(a,e,i,o,u)
 The Second Character must be a English alphabet(it can be lowercase or
uppercase English alphabets)
Regexp By Varun Kumar 3134330169
Page 13 of 50
 The Third Character must a single number
 The Fourth Character should be any character except a Hyphen(-)
 The Fifth Character must be a Hyphen(-)
 The Sixth Character should be any character except a Hyphen(-)
Meta Characters
What are Meta Characters
In regex each character is described as normal character or meta character.
Special character that have special meaning.
Regexp By Varun Kumar 3134330169
Page 14 of 50
 used to preceed a meta char or predefine character class.
^ negation character and used in anchors
^ Is also used for matching a boundary at the start of the line
$ Is used for matching a boundary at the end of the line
. used to match any character aprart from new line
? quantifiers
* quantifiers
+ quantifiers
{} quantifiers
| Alternation
() Grouping
[] Ranges
= Assertions (look ahead, look behind)
! Assertions (look ahead, look behind)
Regexp By Varun Kumar 3134330169
Page 15 of 50
Wild Card Meta Characters Part 1
. meta character is also called wild card metacharacter
To match only one character
Regexp By Varun Kumar 3134330169
Page 16 of 50
New line char is represented by n. Line break.
Regexp By Varun Kumar 3134330169
Page 17 of 50
s whitespace
Matches new line followed by whitespace
b is a boundary between a word and a non word character
Regexp By Varun Kumar 3134330169
Page 18 of 50
Regexp By Varun Kumar 3134330169
Page 19 of 50
Escaping Meta Characters
Meta characters are escaped by
Regexp By Varun Kumar 3134330169
Page 20 of 50
Predifined Character Classes
To capture a 3 digit number within “”
Regexp By Varun Kumar 3134330169
Page 21 of 50
Regexp By Varun Kumar 3134330169
Page 22 of 50
Anchors And Word Boundary
Anchors
Are used match before the beginning or end of
1) String
2) Line
Regexp By Varun Kumar 3134330169
Page 23 of 50
Regexp By Varun Kumar 3134330169
Page 24 of 50
Word Boundary
Is used to match the boundary between word and non-word character.
Regexp By Varun Kumar 3134330169
Page 25 of 50
Captures all word boundaries
All non-word boundary characters.
Regexp By Varun Kumar 3134330169
Page 26 of 50
Quantifiers
In regx there are 4 types of Quantifiers.
A quantifier is used after a charcter or group and decides how the character or group before the quantifier
will occur.
? quantifier
Regexp By Varun Kumar 3134330169
Page 27 of 50
Regexp By Varun Kumar 3134330169
Page 28 of 50
* Quantifier
Matching Example
Q.1) Create a Regular Expression to match a pattern like below -
Regexp By Varun Kumar 3134330169
Page 29 of 50
User has to provide a valid Name of company in an on line Form and it can have
any alphanumeric value.
Hint:
-----
Name of a company can contain alphanumeric characters
Assumption:
-----------
Here, we assume that the names of the company does not contain any other
special characters. If your requirement is to match a company name with special
characters as well, then the RegeX will change accordingly.
Different waves to achieve it.
Regexp By Varun Kumar 3134330169
Page 30 of 50
Regexp By Varun Kumar 3134330169
Page 31 of 50
<!DOCTYPE html>
<html>
<head>
<title>Testing Regular Expressions in HTML forms</title>
</head>
<body>
<h1>Test your Regular Expressions here</h1>
<fieldset>
<legend>Write your Regular expressions</legend>
<form>
<div>
<label for="display-name"> Enter a company name here:
<span class="warning">*(Enter an alpha numeric character.)</span>
</label>
<input type="text" id="display-name" name="ip-display"
pattern="^[0-z][A-zds]*$"
title="Special Characters Not Allowed"/>
</div>
<div>
<input type="submit" class="submit" value="Submit" />
</div>
</form>
</fieldset>
</body>
</html>
Command explanation.
Regexp By Varun Kumar 3134330169
Page 32 of 50
+ Quantifier
Regexp By Varun Kumar 3134330169
Page 33 of 50
{} Quantifiers To Limit Range
{min,} Quantifiers
Regexp By Varun Kumar 3134330169
Page 34 of 50
{min,max} Quantifiers
Regexp By Varun Kumar 3134330169
Page 35 of 50
Greedy Quantifiers
There is a fix for the greedy natures of the quantifiers by making the quantifiers “Lazy” or “Reluctant”
quantifiers.
Regexp By Varun Kumar 3134330169
Page 36 of 50
Lazy Or Reluctant Quantifiers
Alternative For Lazy Quantifiers
When making a quantifier Lazy this will result in increased processing time and this when used in
applications(bulding live applications) will cause lags. This can be over come by using negation characters.
Regexp By Varun Kumar 3134330169
Page 37 of 50
Regexp By Varun Kumar 3134330169
Page 38 of 50
Greedy Quantifiers vs Lazy Quantifiers
Regexp By Varun Kumar 3134330169
Page 39 of 50
In the pattern match of xml tags we have seen earlier the Lazy quantifiers are much better than greedy
quantifiers.
Increasing the performance of the RegeX
The normal behavior of a quantifier is always Greedy and in some cases, Greedy quantifiers can lead to performance issues and in
some cases, Lazy Quantifier can also lead to performance issues.
Note:
If we find that Greedy Quantifiers have performance issue in your search pattern, then try changing it to Lazy Quantifier and see if it
improves the performance. On the other hand, if you have a lazy Quantifier which has a performance issue, try to change it into Greedy
Quantifier to see if the performance increases. We can also try to use the Negation character to improve the performance.
The key to performance is to always remember -
Regexp By Varun Kumar 3134330169
Page 40 of 50
** Greedy matches the longest possible string
** Lazy matches the smallest possible string
So, remember this, if we try to make Greedy quantifier as Lazy then the meaning will change like this -
{min,max}? - Repeat minimum 'min' times and maximum 'max' times, but as few times as possible(lowest is 'min' times)
{min,}? - Repeat minimum 'min' times and maximum any times, but as few times as possible(lowest is 'min' times)
*? - Repeat any number of times, but as few times as possible(lowest is 0 time)
+? - Repeat any number of times, but as few times as possible(lowest is 1 time)
?? - Repeat either 0 time or 1 time, but as few times as possible(lowest is 0 time)
Groups In Regex
1) Improves the redability of regex
2) Groups can be reused in complex expressions.
Group Examples
Regexp By Varun Kumar 3134330169
Page 41 of 50
The above regex is to match word regex and match only 3 numbers after it.
Regexp By Varun Kumar 3134330169
Page 42 of 50
Back Refrencing In Groups
In order to match all the words in the list
Regexp By Varun Kumar 3134330169
Page 43 of 50
Non-Capturing Groups
In order to optimize a regex it is better to make a group non-capturing group it optimizes the regex and
increases the performance.
A group can be made non-capturing by using the following syntax (:?)
Alteration In Groups
Regexp By Varun Kumar 3134330169
Page 44 of 50
Nesting in Groups/Alternation
Regexp By Varun Kumar 3134330169
Page 45 of 50
Assertions
Look Ahead Assertion
Positive Look Ahead Assertion
Regexp By Varun Kumar 3134330169
Page 46 of 50
Negative Look Ahead Assertion
Look Behind Assertions
Positive Look Behind Assertion
Regexp By Varun Kumar 3134330169
Page 47 of 50
Negative Look Behind Assertion
Real Life Scenarios
index.html style.css validation.js
Matching First/Last Name
Rules to be followed for First Last name
Regexp By Varun Kumar 3134330169
Page 48 of 50
Modify the first/last name as shown below
Matching Username
Modify the .js as shown
Matching Passwords
Regexp By Varun Kumar 3134330169
Page 49 of 50
Matching Email
Matching Phone Number
Example From Real World
After learning what I learnt from the start till this point in this document was able to do complex regex directly
in the real world.
Payload
{
"shipments": [
{
"pickup": "1000000061",
"distributionCenter": "CLSCL1",
"packages": [
{
"consigneeAddress": {
"address1": "apt 123",
"address2": "apt 123",
"address3": "SANTIAGO",
"city": "REGION METROPOLITANA",
"country": "CL",
Regexp By Varun Kumar 3134330169
Page 50 of 50
"email": "test@email.com",
"name": "Test Name",
"phone": "5555555",
"state": "REGION METROPOLITANA"
},
"packageDetails": {
"currency": "CLP",
"orderedProduct": "501180",
"packageDesc": "Desc",
"packageId": "TEST1234L1",
"weight": 10,
"weightUom": "G"
}
}
]
}
]
}
Match "shipments": [ from the payload

More Related Content

Similar to Regular expressions

Java Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun UmraoJava Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun Umrao
ssuserd6b1fd
 
Notes of Java
Notes of JavaNotes of Java
Notes of Java
Arun Umrao
 
TypeScipt - Get Started
TypeScipt - Get StartedTypeScipt - Get Started
TypeScipt - Get Started
Krishnanand Sivaraj
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raghu nath
 
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
Andrey Karpov
 
Javascript Deofuscation A manual Approach
Javascript Deofuscation A manual ApproachJavascript Deofuscation A manual Approach
Javascript Deofuscation A manual Approach
Gregory Hanis
 
Javascript
JavascriptJavascript
Javascript
vikram singh
 
Learn matlab primer
Learn matlab primerLearn matlab primer
Learn matlab primer
IndumathyPrabu
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Bernard Loire
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
vip_du
 
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun UmraoAssembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
ssuserd6b1fd
 
20BCT23 – PYTHON PROGRAMMING.pptx
20BCT23 – PYTHON PROGRAMMING.pptx20BCT23 – PYTHON PROGRAMMING.pptx
20BCT23 – PYTHON PROGRAMMING.pptx
gokilabrindha
 
117 A Outline 25
117 A Outline 25117 A Outline 25
117 A Outline 25
wasntgosu
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
wasntgosu
 
Scope, binding, papameter passing techniques
Scope, binding, papameter passing techniquesScope, binding, papameter passing techniques
Scope, binding, papameter passing techniques
CareerMonk Publications
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
Sandy Smith
 

Similar to Regular expressions (20)

Java Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun UmraoJava Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun Umrao
 
Notes of Java
Notes of JavaNotes of Java
Notes of Java
 
TypeScipt - Get Started
TypeScipt - Get StartedTypeScipt - Get Started
TypeScipt - Get Started
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
 
Javascript Deofuscation A manual Approach
Javascript Deofuscation A manual ApproachJavascript Deofuscation A manual Approach
Javascript Deofuscation A manual Approach
 
Javascript
JavascriptJavascript
Javascript
 
Learn matlab primer
Learn matlab primerLearn matlab primer
Learn matlab primer
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Javascript
JavascriptJavascript
Javascript
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun UmraoAssembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
 
20BCT23 – PYTHON PROGRAMMING.pptx
20BCT23 – PYTHON PROGRAMMING.pptx20BCT23 – PYTHON PROGRAMMING.pptx
20BCT23 – PYTHON PROGRAMMING.pptx
 
117 A Outline 25
117 A Outline 25117 A Outline 25
117 A Outline 25
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
 
Scope, binding, papameter passing techniques
Scope, binding, papameter passing techniquesScope, binding, papameter passing techniques
Scope, binding, papameter passing techniques
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
 

More from varun kumar karuna

Publishing AwsLlambda Logs Into SplunkCloud
Publishing AwsLlambda Logs Into SplunkCloudPublishing AwsLlambda Logs Into SplunkCloud
Publishing AwsLlambda Logs Into SplunkCloud
varun kumar karuna
 
BW6AutomationUsingBamboo
BW6AutomationUsingBambooBW6AutomationUsingBamboo
BW6AutomationUsingBamboo
varun kumar karuna
 
TibcoBW6.0
TibcoBW6.0TibcoBW6.0
TibcoBW6.0
varun kumar karuna
 
InvokingRestThroughHttpPalette
InvokingRestThroughHttpPaletteInvokingRestThroughHttpPalette
InvokingRestThroughHttpPalette
varun kumar karuna
 
TableauDevelopment
TableauDevelopmentTableauDevelopment
TableauDevelopment
varun kumar karuna
 
TibcoBE-Development
TibcoBE-DevelopmentTibcoBE-Development
TibcoBE-Development
varun kumar karuna
 

More from varun kumar karuna (7)

Publishing AwsLlambda Logs Into SplunkCloud
Publishing AwsLlambda Logs Into SplunkCloudPublishing AwsLlambda Logs Into SplunkCloud
Publishing AwsLlambda Logs Into SplunkCloud
 
removeNameSpacePrefix
removeNameSpacePrefixremoveNameSpacePrefix
removeNameSpacePrefix
 
BW6AutomationUsingBamboo
BW6AutomationUsingBambooBW6AutomationUsingBamboo
BW6AutomationUsingBamboo
 
TibcoBW6.0
TibcoBW6.0TibcoBW6.0
TibcoBW6.0
 
InvokingRestThroughHttpPalette
InvokingRestThroughHttpPaletteInvokingRestThroughHttpPalette
InvokingRestThroughHttpPalette
 
TableauDevelopment
TableauDevelopmentTableauDevelopment
TableauDevelopment
 
TibcoBE-Development
TibcoBE-DevelopmentTibcoBE-Development
TibcoBE-Development
 

Recently uploaded

Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
GDSC PJATK
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 

Recently uploaded (20)

Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 

Regular expressions

  • 1. Regular Expressions From Beginner To Expert By Varun Date: December 24, 2018 In this document want to show how to master regex. I wasn’t good at regex until I realized its power and the necessity to understand and master it. ---- Avoid writing lines and lines of code by using power of regex. ---- The regex shown here will take you from a simple example to the complex patterns. ---- Lot of tools apart from all the programming languages support regex. ---- I personally know integration tools such as Ca-Api-Gateway, Apigee, TibcoFlogo, Mulesoft where regex can be used and match complex patterns, save unnecessary code for pattern match. ---- Logging slutions like ELK, Splunk support full on regex. ---- Analytical tools such as Tableau support full on regex. ---- Please follow ths document and practice along by the end of it. You will start writing complex regex on your own.
  • 2. Regexp By Varun Kumar 3134330169 Page 1 of 50 Contents Introduction ......................................................................................................................................3 What are regular expressions.....................................................................................................3 Why to learn regular expressions ..............................................................................................3 How regex is created ...................................................................................................................3 Tools Used....................................................................................................................................4 Basic Syntax Regex .....................................................................................................................5 All About Characters .......................................................................................................................5 Character Literals.........................................................................................................................5 Character Classes........................................................................................................................6 Forward Boundary .......................................................................................................................7 Character Range...........................................................................................................................8 Regex Live Example ..................................................................................................................10 Negation Characters ..................................................................................................................12 Meta Characters .............................................................................................................................13 What are Meta Characters .........................................................................................................13 Wild Card Meta Characters Part 1 ............................................................................................15 Escaping Meta Characters ........................................................................................................19 Predifined Character Classes ...................................................................................................20 Anchors And Word Boundary.......................................................................................................22 Anchors.......................................................................................................................................22 Word Boundary ..........................................................................................................................24 Quantifiers ......................................................................................................................................26 ? quantifier..................................................................................................................................26 * Quantifier..................................................................................................................................28 Matching Example......................................................................................................................28 + Quantifier .................................................................................................................................32 {} Quantifiers To Limit Range ...................................................................................................33 {min,} Quantifiers .......................................................................................................................33 {min,max} Quantifiers................................................................................................................34 Greedy Quantifiers.....................................................................................................................35
  • 3. Regexp By Varun Kumar 3134330169 Page 2 of 50 Lazy Or Reluctant Quantifiers...................................................................................................36 Alternative For Lazy Quantifiers...........................................................................................36 Greedy Quantifiers vs Lazy Quantifiers...............................................................................38 Increasing the performance of the RegeX...........................................................................39 Groups In Regex ............................................................................................................................40 Group Examples.........................................................................................................................40 Back Refrencing In Groups.......................................................................................................42 Non-Capturing Groups ..............................................................................................................43 Alteration In Groups ..................................................................................................................43 Nesting in Groups/Alternation ..................................................................................................44 Assertions.......................................................................................................................................45 Look Ahead Assertion ...............................................................................................................45 Positive Look Ahead Assertion ................................................................................................45 Negative Look Ahead Assertion ...............................................................................................46 Look Behind Assertions............................................................................................................46 Positive Look Behind Assertion...............................................................................................46 Negative Look Behind Assertion..............................................................................................47 Real Life Scenarios ........................................................................................................................47 Example From My Project Work ...................................................................................................49
  • 4. Regexp By Varun Kumar 3134330169 Page 3 of 50 Introduction What are regular expressions Why to learn regular expressions Used in all programming languages. Also used in caApiGateway and Apigee For example validating user input into a form How regex is created
  • 5. Regexp By Varun Kumar 3134330169 Page 4 of 50 Tools Used Tools used for learning the regex during the course of this document. Online regex tester https://regex101.com https://regexr.com https://www/regextester.com Offline Regex Testers https://atom.io reach regex by using ctrl+F or below steps
  • 6. Regexp By Varun Kumar 3134330169 Page 5 of 50 Regexp are case sensitive. Basic Syntax Regex Used in all programming languages. Also used in Api Management tools such as, caApiGateway and Apigee All About Characters Character Literals Takes the string as it is.
  • 7. Regexp By Varun Kumar 3134330169 Page 6 of 50 Regexby default is case sensitive. :/Java/gm (Case sensitive) :/Java/gmi (Case insensitive) Regex starts searching from left to right Character Classes Character class are enclosed in [] brackets
  • 8. Regexp By Varun Kumar 3134330169 Page 7 of 50 Forward Boundary >> Forward boundary uses the following pattern ‘b’ say in this you want to match only the word bet and not sub string that may contain bet.
  • 9. Regexp By Varun Kumar 3134330169 Page 8 of 50 Character Range
  • 10. Regexp By Varun Kumar 3134330169 Page 9 of 50 Say there are 5 employes and you want to match only the emp 1,2,3 Write a regex to match the first 3 words. Indexes of alphabets and numbers
  • 11. Regexp By Varun Kumar 3134330169 Page 10 of 50 You needs to match the index when defining the character ranges you need to go from small to big [A-z] This is correct [a-Z] This is wrong. Regex Live Example <!DOCTYPE html> <html> <head> <title>Testing Regular Expressions in HTML forms</title> </head> <body> <h1>Test your Regular Expressions here</h1> <fieldset> <legend>Write your Regular expressions</legend> <form> <div> <label for="display-name"> Prove that you are not a robot: <span class="warning">*(Enter a single English capital letter.)</span> </label> <input type="text" id="display-name" name="ip-display" pattern="[A-Z]" title="Enter any single English capital Letter only"/> </div> <div> <input type="submit" class="submit" value="Submit" /> </div> </form> </fieldset> </body> </html>
  • 12. Regexp By Varun Kumar 3134330169 Page 11 of 50
  • 13. Regexp By Varun Kumar 3134330169 Page 12 of 50 Negation Characters Are represented with a ^ symbol inside [] brackets. [^a-e] will match all characters except a-e Example 1:-  The First Character should be any character except a comma(,)  The Second Character must be a comma(,)  The Third Character should be any character except a comma(,) Example 2:-  The First Character must be a vowel(a,e,i,o,u)  The Second Character must be a English alphabet(it can be lowercase or uppercase English alphabets)
  • 14. Regexp By Varun Kumar 3134330169 Page 13 of 50  The Third Character must a single number  The Fourth Character should be any character except a Hyphen(-)  The Fifth Character must be a Hyphen(-)  The Sixth Character should be any character except a Hyphen(-) Meta Characters What are Meta Characters In regex each character is described as normal character or meta character. Special character that have special meaning.
  • 15. Regexp By Varun Kumar 3134330169 Page 14 of 50 used to preceed a meta char or predefine character class. ^ negation character and used in anchors ^ Is also used for matching a boundary at the start of the line $ Is used for matching a boundary at the end of the line . used to match any character aprart from new line ? quantifiers * quantifiers + quantifiers {} quantifiers | Alternation () Grouping [] Ranges = Assertions (look ahead, look behind) ! Assertions (look ahead, look behind)
  • 16. Regexp By Varun Kumar 3134330169 Page 15 of 50 Wild Card Meta Characters Part 1 . meta character is also called wild card metacharacter To match only one character
  • 17. Regexp By Varun Kumar 3134330169 Page 16 of 50 New line char is represented by n. Line break.
  • 18. Regexp By Varun Kumar 3134330169 Page 17 of 50 s whitespace Matches new line followed by whitespace b is a boundary between a word and a non word character
  • 19. Regexp By Varun Kumar 3134330169 Page 18 of 50
  • 20. Regexp By Varun Kumar 3134330169 Page 19 of 50 Escaping Meta Characters Meta characters are escaped by
  • 21. Regexp By Varun Kumar 3134330169 Page 20 of 50 Predifined Character Classes To capture a 3 digit number within “”
  • 22. Regexp By Varun Kumar 3134330169 Page 21 of 50
  • 23. Regexp By Varun Kumar 3134330169 Page 22 of 50 Anchors And Word Boundary Anchors Are used match before the beginning or end of 1) String 2) Line
  • 24. Regexp By Varun Kumar 3134330169 Page 23 of 50
  • 25. Regexp By Varun Kumar 3134330169 Page 24 of 50 Word Boundary Is used to match the boundary between word and non-word character.
  • 26. Regexp By Varun Kumar 3134330169 Page 25 of 50 Captures all word boundaries All non-word boundary characters.
  • 27. Regexp By Varun Kumar 3134330169 Page 26 of 50 Quantifiers In regx there are 4 types of Quantifiers. A quantifier is used after a charcter or group and decides how the character or group before the quantifier will occur. ? quantifier
  • 28. Regexp By Varun Kumar 3134330169 Page 27 of 50
  • 29. Regexp By Varun Kumar 3134330169 Page 28 of 50 * Quantifier Matching Example Q.1) Create a Regular Expression to match a pattern like below -
  • 30. Regexp By Varun Kumar 3134330169 Page 29 of 50 User has to provide a valid Name of company in an on line Form and it can have any alphanumeric value. Hint: ----- Name of a company can contain alphanumeric characters Assumption: ----------- Here, we assume that the names of the company does not contain any other special characters. If your requirement is to match a company name with special characters as well, then the RegeX will change accordingly. Different waves to achieve it.
  • 31. Regexp By Varun Kumar 3134330169 Page 30 of 50
  • 32. Regexp By Varun Kumar 3134330169 Page 31 of 50 <!DOCTYPE html> <html> <head> <title>Testing Regular Expressions in HTML forms</title> </head> <body> <h1>Test your Regular Expressions here</h1> <fieldset> <legend>Write your Regular expressions</legend> <form> <div> <label for="display-name"> Enter a company name here: <span class="warning">*(Enter an alpha numeric character.)</span> </label> <input type="text" id="display-name" name="ip-display" pattern="^[0-z][A-zds]*$" title="Special Characters Not Allowed"/> </div> <div> <input type="submit" class="submit" value="Submit" /> </div> </form> </fieldset> </body> </html> Command explanation.
  • 33. Regexp By Varun Kumar 3134330169 Page 32 of 50 + Quantifier
  • 34. Regexp By Varun Kumar 3134330169 Page 33 of 50 {} Quantifiers To Limit Range {min,} Quantifiers
  • 35. Regexp By Varun Kumar 3134330169 Page 34 of 50 {min,max} Quantifiers
  • 36. Regexp By Varun Kumar 3134330169 Page 35 of 50 Greedy Quantifiers There is a fix for the greedy natures of the quantifiers by making the quantifiers “Lazy” or “Reluctant” quantifiers.
  • 37. Regexp By Varun Kumar 3134330169 Page 36 of 50 Lazy Or Reluctant Quantifiers Alternative For Lazy Quantifiers When making a quantifier Lazy this will result in increased processing time and this when used in applications(bulding live applications) will cause lags. This can be over come by using negation characters.
  • 38. Regexp By Varun Kumar 3134330169 Page 37 of 50
  • 39. Regexp By Varun Kumar 3134330169 Page 38 of 50 Greedy Quantifiers vs Lazy Quantifiers
  • 40. Regexp By Varun Kumar 3134330169 Page 39 of 50 In the pattern match of xml tags we have seen earlier the Lazy quantifiers are much better than greedy quantifiers. Increasing the performance of the RegeX The normal behavior of a quantifier is always Greedy and in some cases, Greedy quantifiers can lead to performance issues and in some cases, Lazy Quantifier can also lead to performance issues. Note: If we find that Greedy Quantifiers have performance issue in your search pattern, then try changing it to Lazy Quantifier and see if it improves the performance. On the other hand, if you have a lazy Quantifier which has a performance issue, try to change it into Greedy Quantifier to see if the performance increases. We can also try to use the Negation character to improve the performance. The key to performance is to always remember -
  • 41. Regexp By Varun Kumar 3134330169 Page 40 of 50 ** Greedy matches the longest possible string ** Lazy matches the smallest possible string So, remember this, if we try to make Greedy quantifier as Lazy then the meaning will change like this - {min,max}? - Repeat minimum 'min' times and maximum 'max' times, but as few times as possible(lowest is 'min' times) {min,}? - Repeat minimum 'min' times and maximum any times, but as few times as possible(lowest is 'min' times) *? - Repeat any number of times, but as few times as possible(lowest is 0 time) +? - Repeat any number of times, but as few times as possible(lowest is 1 time) ?? - Repeat either 0 time or 1 time, but as few times as possible(lowest is 0 time) Groups In Regex 1) Improves the redability of regex 2) Groups can be reused in complex expressions. Group Examples
  • 42. Regexp By Varun Kumar 3134330169 Page 41 of 50 The above regex is to match word regex and match only 3 numbers after it.
  • 43. Regexp By Varun Kumar 3134330169 Page 42 of 50 Back Refrencing In Groups In order to match all the words in the list
  • 44. Regexp By Varun Kumar 3134330169 Page 43 of 50 Non-Capturing Groups In order to optimize a regex it is better to make a group non-capturing group it optimizes the regex and increases the performance. A group can be made non-capturing by using the following syntax (:?) Alteration In Groups
  • 45. Regexp By Varun Kumar 3134330169 Page 44 of 50 Nesting in Groups/Alternation
  • 46. Regexp By Varun Kumar 3134330169 Page 45 of 50 Assertions Look Ahead Assertion Positive Look Ahead Assertion
  • 47. Regexp By Varun Kumar 3134330169 Page 46 of 50 Negative Look Ahead Assertion Look Behind Assertions Positive Look Behind Assertion
  • 48. Regexp By Varun Kumar 3134330169 Page 47 of 50 Negative Look Behind Assertion Real Life Scenarios index.html style.css validation.js Matching First/Last Name Rules to be followed for First Last name
  • 49. Regexp By Varun Kumar 3134330169 Page 48 of 50 Modify the first/last name as shown below Matching Username Modify the .js as shown Matching Passwords
  • 50. Regexp By Varun Kumar 3134330169 Page 49 of 50 Matching Email Matching Phone Number Example From Real World After learning what I learnt from the start till this point in this document was able to do complex regex directly in the real world. Payload { "shipments": [ { "pickup": "1000000061", "distributionCenter": "CLSCL1", "packages": [ { "consigneeAddress": { "address1": "apt 123", "address2": "apt 123", "address3": "SANTIAGO", "city": "REGION METROPOLITANA", "country": "CL",
  • 51. Regexp By Varun Kumar 3134330169 Page 50 of 50 "email": "test@email.com", "name": "Test Name", "phone": "5555555", "state": "REGION METROPOLITANA" }, "packageDetails": { "currency": "CLP", "orderedProduct": "501180", "packageDesc": "Desc", "packageId": "TEST1234L1", "weight": 10, "weightUom": "G" } } ] } ] } Match "shipments": [ from the payload