SlideShare ist ein Scribd-Unternehmen logo
1 von 56
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
What is a regular expression?
“A string that defines a text
matching pattern”
Jill roll number is 1001
Bob roll number is 1002
Rob roll number is 1003
Jack roll number is 1004 Extract Roll Numbers ?
dddd
Regular Expression
1001
1002
1003
1004
For video lectures, check out
www.facebook.com/CSxFunda
What is the advantage of using
regular expressions?
 Using regular expressions, You can extract
text which follows a pattern by writing only
very few lines of codes
For video lectures, check out
www.facebook.com/CSxFunda
Example
A weight is 46kg
B weight is 54kg
C weight is 60kg
D weight is 70kg
Text File
46
54
60
70
Extract
Without Using Regular Expressions
 Lengthy
Code
 Complex
For video lectures, check out
www.facebook.com/CSxFunda
Example
A weight is 46kg
B weight is 54kg
C weight is 60kg
D weight is 70kg
Text File
46
54
60
70
Extract
Using Regular Expressions
 Less Code
 Simple
For video lectures, check out
www.facebook.com/CSxFunda
Python String
Set of characters enclosed in single or
double quotes
Ex: ‘Kalyan’, “Meghana”
For video lectures, check out
www.facebook.com/CSxFunda
Python Raw Strings
Set of characters enclosed in single or
double quotes preceded by r
Ex: r‘Kalyan’, r“Meghana”
For video lectures, check out
www.facebook.com/CSxFunda
Strings vs Raw Strings
You can write a regular expression as a
string or raw string
In a string regular expression, you have
to escape the special characters.
In a raw string regular expression, you
need not to escape the special
characters
For video lectures, check out
www.facebook.com/CSxFunda
Regular Expressions
 Regular Expression are supported by many programming languages
Ex: Perl, Ruby , Java, Python, Javascript 





 Some languages provide regex capabilities built in
Ex: Perl
 Some languages provide regex capabilities via libraries
Ex: Python
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
re Module
Python supports regular expressions through re
module
That is, you have to import re module for
using regular expressions
import re
No need to explicitly install this module
www.facebook.com/CSxFunda
Steps
Import re module
Write regular expression
Create regex object
Call the function using regex object
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
re Module Functions
 match(text)
 search(text)
 findall(text)
 finditer(text)
 sub(replString, text)
 split(text)
For video lectures, check out
www.facebook.com/CSxFunda
match()
o Looks for the match at the beginning of the string
o Returns a match object if there is a match , otherwise
returns None
regex=re.compile(pattern)
mo=regex.match(text)
For video lectures, check out
www.facebook.com/CSxFunda
search()
o Looks for the match any where in the string
o Returns a match object if there is a match , otherwise
returns None
o If string has more than one match, returns the match
object for the first match only
regex=re.compile(pattern)
mo=regex.search(text)
For video lectures, check out
www.facebook.com/CSxFunda
findall()
o Looks for the match any where in the string
o Returns all matched substrings as a list if there is
match, otherwise returns empty list
regex=re.compile(pattern)
values=regex.findall(text)
For video lectures, check out
www.facebook.com/CSxFunda
finditer()
o Looks for the match any where in the string
o Returns objects for all matched substrings as a list
if there is a match, otherwise returns empty list
regex=re.compile(pattern)
moList=regex.finditer(text)
For video lectures, check out
www.facebook.com/CSxFunda
sub()
o replaces all the matched substrings with the
given replString and returns the modified
string, if there is match
o Returns original string, if there is no match
o Similar to replace option in text editors
regex=re.compile(pattern)
regex.sub(replString,text)
For video lectures, check out
www.facebook.com/CSxFunda
split()
o Looks for match anywhere in the string
o Splits the string at the matched substrings and
returns the splitted string as a list
o Returns original string, if there is no match
o Similar to split() method in strings
regex=re.compile(pattern)
regex.split(text)
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
Groups
o You want to match a substring in a string and want to
extract a part of matched substring, grouping is used.
Match the roll number CS1004 and extract the last four digits
For video lectures, check out
www.facebook.com/CSxFunda
Groups - Types
oNumbered Groups
oNamed Groups
oNon-capturing Groups
For video lectures, check out
www.facebook.com/CSxFunda
Numbered Groups
For video lectures, check out
www.facebook.com/CSxFunda
Named Groups
 When groups are
large in number,
it is difficult
to remember the
group numbers
 In such a case,
we use named
groups
NonCapturing Group(?:)
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
Meta Characters
| (pipe)
? (question mark)
* (asterisk)
+ (plus symbol)
. (dot symbol)
For video lectures, check out
www.facebook.com/CSxFunda
|(pipe)
Matches one of the many characters
Matches
42
100
30
111
A weight is 42kg
B weight is 100kg
C weight is 30kg
D weight is 111kg
For video lectures, check out
www.facebook.com/CSxFunda
?(question mark)
Matches zero or one occurrence
Matches
42
100
30
111
A weight is 42kg
B weight is 100kg
C weight is 30kg
D weight is 111kg
For video lectures, check out
www.facebook.com/CSxFunda
*(asterisk)
Matches zero or more occurrence
Matches
abbbc
abc
ac
abbbc
abc
ac
For video lectures, check out
www.facebook.com/CSxFunda
+(plus symbol)
Matches one or more occurrence
Matches
abbbc
abbc
abc
abbbc
abbc
abc
For video lectures, check out
www.facebook.com/CSxFunda
.(dot symbol)
Matches any character except ‘n’
Matches
Kalyan007
Kalyann007
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
pattern{m}
Matches exactly m repetitions
Matches exactly 3 digits
For video lectures, check out
www.facebook.com/CSxFunda
pattern{m,n}
Matches minimum of m repetitions
& maximum of n repetitions
For video lectures, check out
www.facebook.com/CSxFunda
pattern{m,}
Matches a minimum of m repetitions
Matches exactly 3 digits
Matches exactly 5 digits
Matches exactly 4 digits
Matches exactly 6 digits
.
.
. For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
Greedy Matching
Looks for the maximum possible match
abcabcabcabc Greedy Match
abcabcabcabc
For video lectures, check out
www.facebook.com/CSxFunda
NonGreedy Matching(?)
Looks for the minimum possible match
abcabcabcabc NonGreedy Match
abc
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
Character Classes
Matches one of the many characters
Types
Positive Character Class
Negative Character Class
Shorthand Character Class
For video lectures, check out
www.facebook.com/CSxFunda
Positive Character Class
Matches one of the characters specified in []
[abc] Matches a or b or c
[aeiou] Matches a ,e,i,o,u
[0123456789] Matches numbers 0 to 9
[a-c0-9] Matches a,b,c or 0 to 9
For video lectures, check out
www.facebook.com/CSxFunda
Negative Character Class
Matches any character other than the characters
specified in [^]
[^aeiou] Matches other an aeiou
b1001
c1002
d1003
f1004
h1005
b1001
c1002
d1003
f1004
h1005
For video lectures, check out
www.facebook.com/CSxFunda
Shorthand Character Class
d any decimal digit [0-9]Matches Equivalent to
D Any non- digit [^0-9]Matches Equivalent to
w any word character [a-zA-z_0-9]Matches Equivalent to
W
any non word char
[^a-zA-Z_0-9]Matches Equivalent to
s any space character [ntrfv]Matches Equivalent to
S Any non space char [^ntvrf]Matches Equivalent to
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
Anchoring
Specify the relative location of the match
Anchoring Meaning
^ Start of line or string
$ End of line or string
A Start of string
Z End of string
b Word boundary
B Non word boundary
For video lectures, check out
www.facebook.com/CSxFunda
^ - Start of line or String
Specify the location of the match as “start of
line or string”
r’^Hello’
r’hello’
Matches hello anywhere in
the input string
Matches hello at the
beginning of input string
For video lectures, check out
www.facebook.com/CSxFunda
$ - End of line or String
Specify the location of the match as “end of
line or string”
r’bye$’
r’bye’
Matches bye anywhere in the
input string
Matches bye at the end of
input string
For video lectures, check out
www.facebook.com/CSxFunda
b – Word boundary
Specify the word boundary
Matches any character other than word characters
that is, other than [a-zA-z0-9_]
bcatb
Matches cat in
“My cat”
“Your cat”
“cat1 is good”
“(cat) is pet”
But not in
“Concatenation of strings”
“catalyst is zinc”
For video lectures, check out
www.facebook.com/CSxFunda
B – Non word boundary
Specify the non word boundary
Opposite to b
BcatB
Matches cat in
“Concatenation of strings”
“Acatalyst is zinc”
But not in
“My cat”
“Your cat”
“cat1 is good”
“(cat) is pet”
For video lectures, check out
www.facebook.com/CSxFunda
For video lectures, check out
www.facebook.com/CSxFunda
Compilation Flags
 compile() has two paramters, first one is the
pattern and second one is compilation flag
which is optional.
re.compile(pattern, [flag])
 Compilation flags can be passed to compile() or
it can be embedded in the regex pattern itself
Pattern=r’(?i)w+’
For video lectures, check out
www.facebook.com/CSxFunda
Compilation Flags
Compilation Flag Meaning
re.IGNORECASE OR re.I Case insensitive matching
(i)
re.DOTALL or re.S . Matches any character
including ‘n’ (s)
re.VERBOSE or re.X Ignores white spaces and
comments (x)
re.MULTILINE or re.M Enable multi line mode (m)
re.UNICODE or re.U Enable the Unicode mode (u)
For video lectures, check out
www.facebook.com/CSxFunda
Assertions
Look Ahead Assertions
Positive Look Ahead Assertions
Negative Look Ahead Assertions
Look Behind Assertions
Positive Look Behind Assertions
Negative Look Behind Assertions

Weitere Àhnliche Inhalte

Was ist angesagt?

Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Edureka!
 
Php array
Php arrayPhp array
Php arrayNikul Shah
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Data Representation of Strings
Data Representation of StringsData Representation of Strings
Data Representation of StringsProf Ansari
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In JavaMD SALEEM QAISAR
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and commentsNilimesh Halder
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging moduleP3 InfoTech Solutions Pvt. Ltd.
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 
Data types in php
Data types in phpData types in php
Data types in phpilakkiya
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List Hitesh-Java
 
Race condition
Race conditionRace condition
Race conditionhama7230
 
MongoDB Cheat Sheet – Quick Reference
MongoDB Cheat Sheet – Quick ReferenceMongoDB Cheat Sheet – Quick Reference
MongoDB Cheat Sheet – Quick ReferenceCĂ©sar Trigo
 

Was ist angesagt? (20)

Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
 
Php array
Php arrayPhp array
Php array
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Data Representation of Strings
Data Representation of StringsData Representation of Strings
Data Representation of Strings
 
Java notes
Java notesJava notes
Java notes
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Data types in php
Data types in phpData types in php
Data types in php
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Race condition
Race conditionRace condition
Race condition
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
MongoDB Cheat Sheet – Quick Reference
MongoDB Cheat Sheet – Quick ReferenceMongoDB Cheat Sheet – Quick Reference
MongoDB Cheat Sheet – Quick Reference
 
Js scope
Js scopeJs scope
Js scope
 

KĂŒrzlich hochgeladen

Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectBoston Institute of Analytics
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...amitlee9823
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Call Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night StandCall Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night Standamitlee9823
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 

KĂŒrzlich hochgeladen (20)

Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night StandCall Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 đŸ„” Book Your One night Stand
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 

Python Regular Expressions

  • 1. For video lectures, check out www.facebook.com/CSxFunda
  • 2. For video lectures, check out www.facebook.com/CSxFunda
  • 3. What is a regular expression? “A string that defines a text matching pattern” Jill roll number is 1001 Bob roll number is 1002 Rob roll number is 1003 Jack roll number is 1004 Extract Roll Numbers ? dddd Regular Expression 1001 1002 1003 1004 For video lectures, check out www.facebook.com/CSxFunda
  • 4. What is the advantage of using regular expressions?  Using regular expressions, You can extract text which follows a pattern by writing only very few lines of codes For video lectures, check out www.facebook.com/CSxFunda
  • 5. Example A weight is 46kg B weight is 54kg C weight is 60kg D weight is 70kg Text File 46 54 60 70 Extract Without Using Regular Expressions  Lengthy Code  Complex For video lectures, check out www.facebook.com/CSxFunda
  • 6. Example A weight is 46kg B weight is 54kg C weight is 60kg D weight is 70kg Text File 46 54 60 70 Extract Using Regular Expressions  Less Code  Simple For video lectures, check out www.facebook.com/CSxFunda
  • 7. Python String Set of characters enclosed in single or double quotes Ex: ‘Kalyan’, “Meghana” For video lectures, check out www.facebook.com/CSxFunda
  • 8. Python Raw Strings Set of characters enclosed in single or double quotes preceded by r Ex: r‘Kalyan’, r“Meghana” For video lectures, check out www.facebook.com/CSxFunda
  • 9. Strings vs Raw Strings You can write a regular expression as a string or raw string In a string regular expression, you have to escape the special characters. In a raw string regular expression, you need not to escape the special characters For video lectures, check out www.facebook.com/CSxFunda
  • 10. Regular Expressions  Regular Expression are supported by many programming languages Ex: Perl, Ruby , Java, Python, Javascript 




  Some languages provide regex capabilities built in Ex: Perl  Some languages provide regex capabilities via libraries Ex: Python For video lectures, check out www.facebook.com/CSxFunda
  • 11. For video lectures, check out www.facebook.com/CSxFunda
  • 12. re Module Python supports regular expressions through re module That is, you have to import re module for using regular expressions import re No need to explicitly install this module www.facebook.com/CSxFunda
  • 13. Steps Import re module Write regular expression Create regex object Call the function using regex object For video lectures, check out www.facebook.com/CSxFunda
  • 14. For video lectures, check out www.facebook.com/CSxFunda
  • 15. re Module Functions  match(text)  search(text)  findall(text)  finditer(text)  sub(replString, text)  split(text) For video lectures, check out www.facebook.com/CSxFunda
  • 16. match() o Looks for the match at the beginning of the string o Returns a match object if there is a match , otherwise returns None regex=re.compile(pattern) mo=regex.match(text) For video lectures, check out www.facebook.com/CSxFunda
  • 17. search() o Looks for the match any where in the string o Returns a match object if there is a match , otherwise returns None o If string has more than one match, returns the match object for the first match only regex=re.compile(pattern) mo=regex.search(text) For video lectures, check out www.facebook.com/CSxFunda
  • 18. findall() o Looks for the match any where in the string o Returns all matched substrings as a list if there is match, otherwise returns empty list regex=re.compile(pattern) values=regex.findall(text) For video lectures, check out www.facebook.com/CSxFunda
  • 19. finditer() o Looks for the match any where in the string o Returns objects for all matched substrings as a list if there is a match, otherwise returns empty list regex=re.compile(pattern) moList=regex.finditer(text) For video lectures, check out www.facebook.com/CSxFunda
  • 20. sub() o replaces all the matched substrings with the given replString and returns the modified string, if there is match o Returns original string, if there is no match o Similar to replace option in text editors regex=re.compile(pattern) regex.sub(replString,text) For video lectures, check out www.facebook.com/CSxFunda
  • 21. split() o Looks for match anywhere in the string o Splits the string at the matched substrings and returns the splitted string as a list o Returns original string, if there is no match o Similar to split() method in strings regex=re.compile(pattern) regex.split(text) For video lectures, check out www.facebook.com/CSxFunda
  • 22. For video lectures, check out www.facebook.com/CSxFunda
  • 23. Groups o You want to match a substring in a string and want to extract a part of matched substring, grouping is used. Match the roll number CS1004 and extract the last four digits For video lectures, check out www.facebook.com/CSxFunda
  • 24. Groups - Types oNumbered Groups oNamed Groups oNon-capturing Groups For video lectures, check out www.facebook.com/CSxFunda
  • 25. Numbered Groups For video lectures, check out www.facebook.com/CSxFunda
  • 26. Named Groups  When groups are large in number, it is difficult to remember the group numbers  In such a case, we use named groups
  • 27. NonCapturing Group(?:) For video lectures, check out www.facebook.com/CSxFunda
  • 28. For video lectures, check out www.facebook.com/CSxFunda
  • 29. Meta Characters | (pipe) ? (question mark) * (asterisk) + (plus symbol) . (dot symbol) For video lectures, check out www.facebook.com/CSxFunda
  • 30. |(pipe) Matches one of the many characters Matches 42 100 30 111 A weight is 42kg B weight is 100kg C weight is 30kg D weight is 111kg For video lectures, check out www.facebook.com/CSxFunda
  • 31. ?(question mark) Matches zero or one occurrence Matches 42 100 30 111 A weight is 42kg B weight is 100kg C weight is 30kg D weight is 111kg For video lectures, check out www.facebook.com/CSxFunda
  • 32. *(asterisk) Matches zero or more occurrence Matches abbbc abc ac abbbc abc ac For video lectures, check out www.facebook.com/CSxFunda
  • 33. +(plus symbol) Matches one or more occurrence Matches abbbc abbc abc abbbc abbc abc For video lectures, check out www.facebook.com/CSxFunda
  • 34. .(dot symbol) Matches any character except ‘n’ Matches Kalyan007 Kalyann007 For video lectures, check out www.facebook.com/CSxFunda
  • 35. For video lectures, check out www.facebook.com/CSxFunda
  • 36. pattern{m} Matches exactly m repetitions Matches exactly 3 digits For video lectures, check out www.facebook.com/CSxFunda
  • 37. pattern{m,n} Matches minimum of m repetitions & maximum of n repetitions For video lectures, check out www.facebook.com/CSxFunda
  • 38. pattern{m,} Matches a minimum of m repetitions Matches exactly 3 digits Matches exactly 5 digits Matches exactly 4 digits Matches exactly 6 digits . . . For video lectures, check out www.facebook.com/CSxFunda
  • 39. For video lectures, check out www.facebook.com/CSxFunda
  • 40. Greedy Matching Looks for the maximum possible match abcabcabcabc Greedy Match abcabcabcabc For video lectures, check out www.facebook.com/CSxFunda
  • 41. NonGreedy Matching(?) Looks for the minimum possible match abcabcabcabc NonGreedy Match abc For video lectures, check out www.facebook.com/CSxFunda
  • 42. For video lectures, check out www.facebook.com/CSxFunda
  • 43. Character Classes Matches one of the many characters Types Positive Character Class Negative Character Class Shorthand Character Class For video lectures, check out www.facebook.com/CSxFunda
  • 44. Positive Character Class Matches one of the characters specified in [] [abc] Matches a or b or c [aeiou] Matches a ,e,i,o,u [0123456789] Matches numbers 0 to 9 [a-c0-9] Matches a,b,c or 0 to 9 For video lectures, check out www.facebook.com/CSxFunda
  • 45. Negative Character Class Matches any character other than the characters specified in [^] [^aeiou] Matches other an aeiou b1001 c1002 d1003 f1004 h1005 b1001 c1002 d1003 f1004 h1005 For video lectures, check out www.facebook.com/CSxFunda
  • 46. Shorthand Character Class d any decimal digit [0-9]Matches Equivalent to D Any non- digit [^0-9]Matches Equivalent to w any word character [a-zA-z_0-9]Matches Equivalent to W any non word char [^a-zA-Z_0-9]Matches Equivalent to s any space character [ntrfv]Matches Equivalent to S Any non space char [^ntvrf]Matches Equivalent to For video lectures, check out www.facebook.com/CSxFunda
  • 47. For video lectures, check out www.facebook.com/CSxFunda
  • 48. Anchoring Specify the relative location of the match Anchoring Meaning ^ Start of line or string $ End of line or string A Start of string Z End of string b Word boundary B Non word boundary For video lectures, check out www.facebook.com/CSxFunda
  • 49. ^ - Start of line or String Specify the location of the match as “start of line or string” r’^Hello’ r’hello’ Matches hello anywhere in the input string Matches hello at the beginning of input string For video lectures, check out www.facebook.com/CSxFunda
  • 50. $ - End of line or String Specify the location of the match as “end of line or string” r’bye$’ r’bye’ Matches bye anywhere in the input string Matches bye at the end of input string For video lectures, check out www.facebook.com/CSxFunda
  • 51. b – Word boundary Specify the word boundary Matches any character other than word characters that is, other than [a-zA-z0-9_] bcatb Matches cat in “My cat” “Your cat” “cat1 is good” “(cat) is pet” But not in “Concatenation of strings” “catalyst is zinc” For video lectures, check out www.facebook.com/CSxFunda
  • 52. B – Non word boundary Specify the non word boundary Opposite to b BcatB Matches cat in “Concatenation of strings” “Acatalyst is zinc” But not in “My cat” “Your cat” “cat1 is good” “(cat) is pet” For video lectures, check out www.facebook.com/CSxFunda
  • 53. For video lectures, check out www.facebook.com/CSxFunda
  • 54. Compilation Flags  compile() has two paramters, first one is the pattern and second one is compilation flag which is optional. re.compile(pattern, [flag])  Compilation flags can be passed to compile() or it can be embedded in the regex pattern itself Pattern=r’(?i)w+’ For video lectures, check out www.facebook.com/CSxFunda
  • 55. Compilation Flags Compilation Flag Meaning re.IGNORECASE OR re.I Case insensitive matching (i) re.DOTALL or re.S . Matches any character including ‘n’ (s) re.VERBOSE or re.X Ignores white spaces and comments (x) re.MULTILINE or re.M Enable multi line mode (m) re.UNICODE or re.U Enable the Unicode mode (u) For video lectures, check out www.facebook.com/CSxFunda
  • 56. Assertions Look Ahead Assertions Positive Look Ahead Assertions Negative Look Ahead Assertions Look Behind Assertions Positive Look Behind Assertions Negative Look Behind Assertions