SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Regular Expressions
Prelude
What are Regular Expressions?
• Regular expressions are a powerful language for
matching text patterns.
• It is standardized way of searching, replacing, and
parsing text with complex patterns of characters.
• All modern languages have similar library packages
• for regular expressions i.e., re built in module
History
History
History
RegEx Features
• Hundreds of code could be reduced to a one line
elegant regular expression.
• Used to construct compilers, interpreters and text
editors.
• Used to search and match text patterns.
• Used to validate text data formats especially input data.
• Popular programming languages have Regex
capabilities. Ex: Python, Perl, JavaScript, Ruby, C++,C#
RegEx Features
General uses of regular expressions are to:-
• Search a string (search and match)
• Replace parts of a string (sub)
• Break string into small pieces (split)
• Finding a string (findall)
Literal Characters
Literal Characters
Literal Characters
Special Characters
General Concepts
Alternative & Grouping
Alternative:-
• Ex: “cat|mat” ==“cat” or “mat”
• “python|jython” ==“python” or “jython”
Grouping:-
• Ex: gr(e|a)y==“grey” or “gray”
• “ra(mil|n(ny|el))”==“ramil” or “ranny” or “ranel”
Quantification
• * => zero or more of the preceding element
– Ex: “fo*ot”==“fot”, “foot” or “fooot” or “foooooot”
– “94*9” ==“99” or “9449” or “9444449”
• + => one or more of the preceding element
– Ex: “too+fan”==“toofan” or “tooooofan”
– “36+40”==“3640” or “3666640”
Quantification
• ? => zero or one of the preceding element
– Ex: “rani?el”==“raniel” or “ranel”
– “colou?r”==“colour” or “color”
• {m,n} => m to n times of the preceding element
– Ex: “go{2,3}gle”== “google” or “gooogle”
– “6{3} “==“666”
– “s{2,}”==“ss” or “sss” or “ssss” ……
Anchors
• ^ => matches the starting position with in the
string
– Ex: “^obje”==“object” or “object – oriented”
– “^2014”==“2014” or “2014/20/07”
• $ => matches the ending position with in the
string
– Ex: “gram$”==“program” or “kilogram”
– “2014$” == “20/07/2014”, “2013-2014”
Meta-characters
• .(dot) => matches any single character
– Ex: “bat.”== “bat” or “bats” or “bata”
– “87.1”==“8741” or “8751” or “8761”
• [ ] => matches a single character that is
contained with in the brackets
– Ex: “[xyz]” == “x” or “y” or “z”
– “[aeiou]”==any vowel
– “[0123456789]”==any digit
– “[Gg]r[ae]y” == Gray or grey
Meta-characters
• [ - ] => matches a single character that is contained
within the brackets and the specified range.
– Ex: “[a-c]” == “a” or “b” or “c”
– “[a-zA-Z]” == all letters (lowercase & uppercase)
– “[0-9]” == all digits
• [^ ] => matches a single character that is not contained
within the brackets.
– “[^aeiou]” == any non-vowel
– “[^0-9]” == any non-digit
– “[^xyz]” == any character, but not “x”, “y”, or “z”
Character Classes
Character classes specifies a group of characters to
match in a string:->
• d - a decimal digit [0-9]
• D - non digits [^d]
• s - single whitespace or character [t- tab,n-
newline, r- return,v- space, f- form]
• S - non-white space character [^s]
Character Classes
• w - alphanumeric character ([a-zA-Z0-9_])
• W - non-alphanumeric character ([^a-zA-Z0-9_])
• w+ - one or more words / characters
• b - word boundaries when outside brackets.
• B - non-word boundaries
• A - beginning of string
• z - end of string
Search Class
• Search scans through the input string and tries to
match at any location.
• The search function searches for first occurrence of
RE pattern within string with optional flags.
• Syntax: re.search(pattern, string, flags=0)
– Pattern --This is the regular expression to be matched
– String-- This is the string, which would be searched to
match the pattern anywhere in the string
– Flags-- You can specify different flags using bitwise OR
(|). These are modifiers.
Search Class
• The re.search function returns a match object on
success, None on failure.
• match object for information about the matching string.
• match object instances also have several methods
and attributes; the most important ones are:-
Search Class
Greediness:-
• It means that the engine
will always try to match
as much as possible.
• Ex: anS+ results in
A robot and an android
fight. The ninja wins.
Laziness:-
• It means that the engine
will always try to match
as much as it needs to.
• Ex: anS+? results in
A robot and an android
fight. The ninja wins.
Modifying Strings
• Regular expressions are also commonly used to
modify strings in various ways using the following
pattern methods:-
Modifying Strings
• Split Method: searching for pattern matches or
performing string substitutions
Modifying Strings
• Sub Method: It replaces all occurrences of the RE
pattern in string with repl, substituting all
occurrences unless max provided.
• This method would return modified string
• Syntax: re.sub(pattern, repl, string, max=0)
Modifying Strings
• Findall: It return all non-overlapping matches of
pattern in string, as a list of strings.
• The string is scanned left-to-right, and matches are
returned in the order found.
• Syntax: re.findall (pattern, string, flags=0)
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

Weitere ähnliche Inhalte

Was ist angesagt?

Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsMesut Günes
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Edureka!
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4sumitbardhan
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulationIntro C# Book
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and ArraysWebStackAcademy
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaGlenn Guden
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptxParvizMirzayev2
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 

Was ist angesagt? (20)

Java threads
Java threadsJava threads
Java threads
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Java I/O
Java I/OJava I/O
Java I/O
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 

Ähnlich wie Regular Expressions Explained

Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeProf. Wim Van Criekinge
 
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
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
CiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Ahmed El-Arabawy
 
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
Regular expressionsRegular expressions
Regular expressionsEran Zimbler
 
Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...Allison Jai O'Dell
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GISJohn Reiser
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxmythili213835
 
Unit 2 - Regular Expression .pptx
Unit 2 - Regular        Expression .pptxUnit 2 - Regular        Expression .pptx
Unit 2 - Regular Expression .pptxmythili213835
 

Ähnlich wie Regular Expressions Explained (20)

Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expression for everyone
Regular expression for everyoneRegular expression for everyone
Regular expression for everyone
 
2.regular expressions
2.regular expressions2.regular expressions
2.regular expressions
 
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
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
CiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex PresentationCiNPA Security SIG - Regex Presentation
CiNPA Security SIG - Regex Presentation
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: 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
 
Regex posix
Regex posixRegex posix
Regex posix
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...Understanding Regular expressions: Programming Historian Study Group, Univers...
Understanding Regular expressions: Programming Historian Study Group, Univers...
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GIS
 
Unit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptxUnit 2 - Regular Expression.pptx
Unit 2 - Regular Expression.pptx
 
Unit 2 - Regular Expression .pptx
Unit 2 - Regular        Expression .pptxUnit 2 - Regular        Expression .pptx
Unit 2 - Regular Expression .pptx
 

Mehr von Akhil Kaushik

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationAkhil Kaushik
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer ImplementationAkhil Kaushik
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignAkhil Kaushik
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsAkhil Kaushik
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & LoopsAkhil Kaushik
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in PythonAkhil Kaushik
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in CompilerAkhil Kaushik
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction toolsAkhil Kaushik
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to CompilersAkhil Kaushik
 

Mehr von Akhil Kaushik (20)

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer Implementation
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in Python
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 

Kürzlich hochgeladen

Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 

Kürzlich hochgeladen (20)

Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 

Regular Expressions Explained

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Regular Expressions
  • 3. What are Regular Expressions? • Regular expressions are a powerful language for matching text patterns. • It is standardized way of searching, replacing, and parsing text with complex patterns of characters. • All modern languages have similar library packages • for regular expressions i.e., re built in module
  • 7. RegEx Features • Hundreds of code could be reduced to a one line elegant regular expression. • Used to construct compilers, interpreters and text editors. • Used to search and match text patterns. • Used to validate text data formats especially input data. • Popular programming languages have Regex capabilities. Ex: Python, Perl, JavaScript, Ruby, C++,C#
  • 8. RegEx Features General uses of regular expressions are to:- • Search a string (search and match) • Replace parts of a string (sub) • Break string into small pieces (split) • Finding a string (findall)
  • 14. Alternative & Grouping Alternative:- • Ex: “cat|mat” ==“cat” or “mat” • “python|jython” ==“python” or “jython” Grouping:- • Ex: gr(e|a)y==“grey” or “gray” • “ra(mil|n(ny|el))”==“ramil” or “ranny” or “ranel”
  • 15. Quantification • * => zero or more of the preceding element – Ex: “fo*ot”==“fot”, “foot” or “fooot” or “foooooot” – “94*9” ==“99” or “9449” or “9444449” • + => one or more of the preceding element – Ex: “too+fan”==“toofan” or “tooooofan” – “36+40”==“3640” or “3666640”
  • 16. Quantification • ? => zero or one of the preceding element – Ex: “rani?el”==“raniel” or “ranel” – “colou?r”==“colour” or “color” • {m,n} => m to n times of the preceding element – Ex: “go{2,3}gle”== “google” or “gooogle” – “6{3} “==“666” – “s{2,}”==“ss” or “sss” or “ssss” ……
  • 17. Anchors • ^ => matches the starting position with in the string – Ex: “^obje”==“object” or “object – oriented” – “^2014”==“2014” or “2014/20/07” • $ => matches the ending position with in the string – Ex: “gram$”==“program” or “kilogram” – “2014$” == “20/07/2014”, “2013-2014”
  • 18. Meta-characters • .(dot) => matches any single character – Ex: “bat.”== “bat” or “bats” or “bata” – “87.1”==“8741” or “8751” or “8761” • [ ] => matches a single character that is contained with in the brackets – Ex: “[xyz]” == “x” or “y” or “z” – “[aeiou]”==any vowel – “[0123456789]”==any digit – “[Gg]r[ae]y” == Gray or grey
  • 19. Meta-characters • [ - ] => matches a single character that is contained within the brackets and the specified range. – Ex: “[a-c]” == “a” or “b” or “c” – “[a-zA-Z]” == all letters (lowercase & uppercase) – “[0-9]” == all digits • [^ ] => matches a single character that is not contained within the brackets. – “[^aeiou]” == any non-vowel – “[^0-9]” == any non-digit – “[^xyz]” == any character, but not “x”, “y”, or “z”
  • 20. Character Classes Character classes specifies a group of characters to match in a string:-> • d - a decimal digit [0-9] • D - non digits [^d] • s - single whitespace or character [t- tab,n- newline, r- return,v- space, f- form] • S - non-white space character [^s]
  • 21. Character Classes • w - alphanumeric character ([a-zA-Z0-9_]) • W - non-alphanumeric character ([^a-zA-Z0-9_]) • w+ - one or more words / characters • b - word boundaries when outside brackets. • B - non-word boundaries • A - beginning of string • z - end of string
  • 22. Search Class • Search scans through the input string and tries to match at any location. • The search function searches for first occurrence of RE pattern within string with optional flags. • Syntax: re.search(pattern, string, flags=0) – Pattern --This is the regular expression to be matched – String-- This is the string, which would be searched to match the pattern anywhere in the string – Flags-- You can specify different flags using bitwise OR (|). These are modifiers.
  • 23. Search Class • The re.search function returns a match object on success, None on failure. • match object for information about the matching string. • match object instances also have several methods and attributes; the most important ones are:-
  • 24. Search Class Greediness:- • It means that the engine will always try to match as much as possible. • Ex: anS+ results in A robot and an android fight. The ninja wins. Laziness:- • It means that the engine will always try to match as much as it needs to. • Ex: anS+? results in A robot and an android fight. The ninja wins.
  • 25. Modifying Strings • Regular expressions are also commonly used to modify strings in various ways using the following pattern methods:-
  • 26. Modifying Strings • Split Method: searching for pattern matches or performing string substitutions
  • 27. Modifying Strings • Sub Method: It replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. • This method would return modified string • Syntax: re.sub(pattern, repl, string, max=0)
  • 28. Modifying Strings • Findall: It return all non-overlapping matches of pattern in string, as a list of strings. • The string is scanned left-to-right, and matches are returned in the order found. • Syntax: re.findall (pattern, string, flags=0)
  • 29. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!