SlideShare a Scribd company logo
1 of 42
Programming with Sikander
CorporateTrainer: C, Modern C++, Python, Linux System Prog
Programming with Sikander : Python: Regular Expression
 Regular expressions are a powerful tool
for various kinds of string manipulation.
 They are a domain specific language
(DSL) that is present as a library in most
modern programming languages, not just
Python.
 They are useful for two main tasks:
 identify whether a pattern exists in a given
sequence of characters (string) or not.
 performing substitutions in a string.
Programming with Sikander : Python: Regular Expression
 Implemented in Python with the “re” module
 import re
Programming with Sikander : Python: Regular Expression
 re.match
 re.search
 re.findall
 re.finditer
 re.fullmatch
Programming with Sikander : Python: Regular Expression
 re.match function can be used to determine
whether pattern matches at the beginning of
a string.
 If it does, match returns an object
representing the match, if not, it returns
None.
 re.match(pattern, sequence)
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
 The function re.search finds a match of a
pattern anywhere in the string.
Programming with Sikander : Python: Regular Expression
 The search function returns an object with
several methods that give details about it.
These methods include
 group which returns the string matched.
 start and end which return the start and
ending positions of the first match
 span which returns the start and end
positions of the first match as a tuple.
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
 The function re.findall returns a list of all
substrings that match a pattern.
Programming with Sikander : Python: Regular Expression
 re.finditer(pattern, string)
 Return an iterator yielding match objects over all
non-overlapping matches for the pattern in string.
 The string is scanned left-to-right, and matches are
returned in the order found.
Programming with Sikander : Python: Regular Expression
Example
Description
Character
"[a-m]"
A set of characters
[]
"d"
Signals a special sequence (can also be used to
escape special characters)

"he..o"
Any character (except newline character)
.
"^hello"
Starts with
^
"world$"
Ends with
$
"aix*"
Zero or more occurrences
*
"aix+"
One or more occurrences
+
"al{2}"
Excactly the specified number of occurrences
{}
"falls|stays"
Either or
|
Capture and group
()
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
Print all the vowels and its index
Input : sikander
Output :
1 i
3 a
6 e
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
Matches any decimal digit; this is equivalent to the class [0-9].
d
Matches any non-digit character; this is equivalent to the class [^0-9].
D
Matches any whitespace character; this is equivalent to the class [ tnr].
s
Matches any non-whitespace character; this is equivalent to the
class [^ tnr].
S
Matches any alphanumeric character; this is equivalent to the class [a-
zA-Z0-9_].
w
Matches any non-alphanumeric character; this is equivalent to the
class [^a-zA-Z0-9_].
W
Programming with Sikander : Python: Regular Expression
 The expression d matches any digit [0-9]
 The expression D matches any character that is
not a digit.
 Given a String extract All the digits and non-digits.
Output
Input
Digits : 1 0 2 0 2 3
Non Digits : R V E C
1RV02EC023
Digits : 1 2 3 4
Non Digits : A B C D E F
ABCDE1234F
Digits : 2 0 0 0
Non Digits : R u p e e s
Rupees 2000
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
• s matches any whitespace character [ tnr]
• S matches any non-white space character.
 Given a String extract all spaces and Non
space characters.
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
• The expression w will match any word
character.
• Word characters include alphanumeric
characters (a-z,A-Z, 0-9) and underscore(_)
• Given a string, extract all word and non-word
characters (remove all special characters)
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
 The ^ symbol matches the position at the
start of a string.
 The $ symbol matches the position at the
end of a string.
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
• One or more occurrences(+) of digits
Programming with Sikander : Python: Regular Expression
• You are given a list of phone numbers and you are
required to check whether they are valid mobile
numbers.
• A valid mobile number is a ten digit number starting
with a 7, 8 or 9.
Programming with Sikander : Python: Regular Expression
• Verify if the given PAN number is correct.
• PAN Number:
 It’s a 10 letter string
 First 5 characters are alphabets
 Next 4 characters are digits
 Last character is alphabet
Programming with Sikander : Python: Regular Expression
Verify if the given USN number is correct.
Programming with Sikander : Python: Regular Expression
• Pattern = “word1|word2|word3”
• Verify if the sequence contains “from” or “to”
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
A group() expression returns one or more
subgroups of the match.
A groups() expression returns a tuple
containing all the subgroups of the match.
Programming with Sikander : Python: Regular Expression
 Given an email-id seperate the username,
website and extension
Programming with Sikander : Python: Regular Expression
A groupdict() expression returns a dictionary
containing all the named subgroups of the match,
keyed by the subgroup name.
Programming with Sikander : Python: Regular Expression
 re.sub (pattern, repl, string)
 Returns the string obtained by replacing
the pattern in string by the replacement
repl.
Programming with Sikander : Python: Regular Expression
Bangalore is the capital of Karnataka.
The Silicon City of India is Bangalore.
Bangalore was called garden city because of its greenary.
Task: Replace all the occurance of Bangalore to Bengaluru.
Programming with Sikander : Python: Regular Expression
 re.compile(pattern, flags=0)
 Compile a regular expression pattern into a
regular expression object, which can be used for
matching using its match(), search() and other
methods.
 It also helps to search a pattern again without
rewriting it.
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression
Programming with Sikander : Python: Regular Expression

More Related Content

What's hot (20)

Emcpp0506
Emcpp0506Emcpp0506
Emcpp0506
 
C function presentation
C function presentationC function presentation
C function presentation
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
Python basic
Python basicPython basic
Python basic
 
Lecture 4 variables data types and operators
Lecture 4  variables data types and operatorsLecture 4  variables data types and operators
Lecture 4 variables data types and operators
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Workshop Swift
Workshop Swift Workshop Swift
Workshop Swift
 
Preprocessors
Preprocessors Preprocessors
Preprocessors
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Tech talks#6: Code Refactoring
Tech talks#6: Code RefactoringTech talks#6: Code Refactoring
Tech talks#6: Code Refactoring
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parameters
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
C functions
C functionsC functions
C functions
 
Refactoring
RefactoringRefactoring
Refactoring
 

Similar to Python_Regular Expression

STRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxSTRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxTinku91
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfpaijitk
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdfGaneshRaghu4
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginnersAbishek Purushothaman
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptxPadreBhoj
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)bolovv
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsMegha V
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
RegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing ExamplesRegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing Exampleszeteo12
 

Similar to Python_Regular Expression (20)

Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
STRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxSTRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptx
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Python basic
Python basicPython basic
Python basic
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
RegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing ExamplesRegEx : Expressions and Parsing Examples
RegEx : Expressions and Parsing Examples
 
Ch09
Ch09Ch09
Ch09
 

More from Mohammed Sikander (18)

Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Python strings
Python stringsPython strings
Python strings
 
Python set
Python setPython set
Python set
 
Python list
Python listPython list
Python list
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
 
Signal
SignalSignal
Signal
 
File management
File managementFile management
File management
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java strings
Java   stringsJava   strings
Java strings
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
Questions typedef and macros
Questions typedef and macrosQuestions typedef and macros
Questions typedef and macros
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 

Recently uploaded

Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
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
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 

Recently uploaded (20)

Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
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
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 

Python_Regular Expression

  • 1. Programming with Sikander CorporateTrainer: C, Modern C++, Python, Linux System Prog
  • 2. Programming with Sikander : Python: Regular Expression  Regular expressions are a powerful tool for various kinds of string manipulation.  They are a domain specific language (DSL) that is present as a library in most modern programming languages, not just Python.  They are useful for two main tasks:  identify whether a pattern exists in a given sequence of characters (string) or not.  performing substitutions in a string.
  • 3. Programming with Sikander : Python: Regular Expression  Implemented in Python with the “re” module  import re
  • 4. Programming with Sikander : Python: Regular Expression  re.match  re.search  re.findall  re.finditer  re.fullmatch
  • 5. Programming with Sikander : Python: Regular Expression  re.match function can be used to determine whether pattern matches at the beginning of a string.  If it does, match returns an object representing the match, if not, it returns None.  re.match(pattern, sequence)
  • 6. Programming with Sikander : Python: Regular Expression
  • 7. Programming with Sikander : Python: Regular Expression
  • 8. Programming with Sikander : Python: Regular Expression  The function re.search finds a match of a pattern anywhere in the string.
  • 9. Programming with Sikander : Python: Regular Expression  The search function returns an object with several methods that give details about it. These methods include  group which returns the string matched.  start and end which return the start and ending positions of the first match  span which returns the start and end positions of the first match as a tuple.
  • 10. Programming with Sikander : Python: Regular Expression
  • 11. Programming with Sikander : Python: Regular Expression
  • 12. Programming with Sikander : Python: Regular Expression  The function re.findall returns a list of all substrings that match a pattern.
  • 13. Programming with Sikander : Python: Regular Expression  re.finditer(pattern, string)  Return an iterator yielding match objects over all non-overlapping matches for the pattern in string.  The string is scanned left-to-right, and matches are returned in the order found.
  • 14. Programming with Sikander : Python: Regular Expression Example Description Character "[a-m]" A set of characters [] "d" Signals a special sequence (can also be used to escape special characters) "he..o" Any character (except newline character) . "^hello" Starts with ^ "world$" Ends with $ "aix*" Zero or more occurrences * "aix+" One or more occurrences + "al{2}" Excactly the specified number of occurrences {} "falls|stays" Either or | Capture and group ()
  • 15. Programming with Sikander : Python: Regular Expression
  • 16. Programming with Sikander : Python: Regular Expression Print all the vowels and its index Input : sikander Output : 1 i 3 a 6 e
  • 17. Programming with Sikander : Python: Regular Expression
  • 18. Programming with Sikander : Python: Regular Expression Matches any decimal digit; this is equivalent to the class [0-9]. d Matches any non-digit character; this is equivalent to the class [^0-9]. D Matches any whitespace character; this is equivalent to the class [ tnr]. s Matches any non-whitespace character; this is equivalent to the class [^ tnr]. S Matches any alphanumeric character; this is equivalent to the class [a- zA-Z0-9_]. w Matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_]. W
  • 19. Programming with Sikander : Python: Regular Expression  The expression d matches any digit [0-9]  The expression D matches any character that is not a digit.  Given a String extract All the digits and non-digits. Output Input Digits : 1 0 2 0 2 3 Non Digits : R V E C 1RV02EC023 Digits : 1 2 3 4 Non Digits : A B C D E F ABCDE1234F Digits : 2 0 0 0 Non Digits : R u p e e s Rupees 2000
  • 20. Programming with Sikander : Python: Regular Expression
  • 21. Programming with Sikander : Python: Regular Expression • s matches any whitespace character [ tnr] • S matches any non-white space character.  Given a String extract all spaces and Non space characters.
  • 22. Programming with Sikander : Python: Regular Expression
  • 23. Programming with Sikander : Python: Regular Expression • The expression w will match any word character. • Word characters include alphanumeric characters (a-z,A-Z, 0-9) and underscore(_) • Given a string, extract all word and non-word characters (remove all special characters)
  • 24. Programming with Sikander : Python: Regular Expression
  • 25. Programming with Sikander : Python: Regular Expression  The ^ symbol matches the position at the start of a string.  The $ symbol matches the position at the end of a string.
  • 26. Programming with Sikander : Python: Regular Expression
  • 27. Programming with Sikander : Python: Regular Expression
  • 28. Programming with Sikander : Python: Regular Expression • One or more occurrences(+) of digits
  • 29. Programming with Sikander : Python: Regular Expression • You are given a list of phone numbers and you are required to check whether they are valid mobile numbers. • A valid mobile number is a ten digit number starting with a 7, 8 or 9.
  • 30. Programming with Sikander : Python: Regular Expression • Verify if the given PAN number is correct. • PAN Number:  It’s a 10 letter string  First 5 characters are alphabets  Next 4 characters are digits  Last character is alphabet
  • 31. Programming with Sikander : Python: Regular Expression Verify if the given USN number is correct.
  • 32. Programming with Sikander : Python: Regular Expression • Pattern = “word1|word2|word3” • Verify if the sequence contains “from” or “to”
  • 33. Programming with Sikander : Python: Regular Expression
  • 34. Programming with Sikander : Python: Regular Expression A group() expression returns one or more subgroups of the match. A groups() expression returns a tuple containing all the subgroups of the match.
  • 35. Programming with Sikander : Python: Regular Expression  Given an email-id seperate the username, website and extension
  • 36. Programming with Sikander : Python: Regular Expression A groupdict() expression returns a dictionary containing all the named subgroups of the match, keyed by the subgroup name.
  • 37. Programming with Sikander : Python: Regular Expression  re.sub (pattern, repl, string)  Returns the string obtained by replacing the pattern in string by the replacement repl.
  • 38. Programming with Sikander : Python: Regular Expression Bangalore is the capital of Karnataka. The Silicon City of India is Bangalore. Bangalore was called garden city because of its greenary. Task: Replace all the occurance of Bangalore to Bengaluru.
  • 39. Programming with Sikander : Python: Regular Expression  re.compile(pattern, flags=0)  Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search() and other methods.  It also helps to search a pattern again without rewriting it.
  • 40. Programming with Sikander : Python: Regular Expression
  • 41. Programming with Sikander : Python: Regular Expression
  • 42. Programming with Sikander : Python: Regular Expression