SlideShare a Scribd company logo
1 of 20
Download to read offline
Regular
Expressions
Expressions
Regular
Expressions
Expressions
What is Regular Expression
A Regular Expression (RegEx) is a sequence of characters that defines a
search pattern. For example,
^a……s$
^a……s$
The above code defines a RegEx pattern. The pattern is:
letter string starting with a and ending with s.
What is Regular Expression
) is a sequence of characters that defines a
^a……s$
^a……s$
pattern. The pattern is: any five
with a and ending with s.
Python has a module named re to work with regular expression. Here’s an example:
import re
pattern = ‘^a…s$’
test_string = ‘abyss’
Result = re.match(pattern, test_string
if result:
print(“Search successful.”)
else:
print(“Search unsuccessful”)
Here, we used re.match function to grab pattern within the
The method returns a match object if the search is successful. If not, it returns
to work with regular expression. Here’s an example:
test_string)
function to grab pattern within the test_string.
The method returns a match object if the search is successful. If not, it returns No
Specify Pattern Using RegEx:
To specify regular expressions, metacharacters are used. In the above
example, ^ and $ are metacharacters.
Meta characters:
Metacharacters are characters that are interpreted in a special way by a
RegEx engine. Here's a list of metacharacters:
[] . ^ $ *
[] – Square brackets:
Square brackets specifies a set of characters you wish to match.
Square brackets specifies a set of characters you wish to match.
To specify regular expressions, metacharacters are used. In the above
Metacharacters are characters that are interpreted in a special way by a
engine. Here's a list of metacharacters:
Square brackets specifies a set of characters you wish to match.
Square brackets specifies a set of characters you wish to match.
Above, [abc] will match if the string you are trying to match contains any
of the a, b or c.
You can also specify a range of characters using
o [a-e] is the same as [abcde].
o [1-4] is the same as [1234].
[0-39] is the same as [01239].
o
o [0-39] is the same as [01239].
You can complement (invert) the character set by using caret ^ symbol at
the start of a square-bracket.
o [^abc] means any character except a or b or c.
o [^0-9] means any non-digit character.
] will match if the string you are trying to match contains any
You can also specify a range of characters using – inside square brackets.
You can complement (invert) the character set by using caret ^ symbol at
] means any character except a or b or c.
digit character.
– Period:
A period matches any single character (except newline
A period matches any single character (except newline ’n’).
^ – Caret:
The caret symbol ^ is used to check if a string starts with a certain character.
The caret symbol ^ is used to check if a string starts with a certain character.
$ – Dollar:
The dollar symbol $ is used to check if a string ends with a certain character.
The dollar symbol $ is used to check if a string ends with a certain character.
+ – Plus:
The plus symbol + matches one or more occurrences of the pattern left to it
The plus symbol + matches one or more occurrences of the pattern left to it.
Python
Python has a module named re to work with regular expressions. To use it,
we need to import the module.
import re
import re
The module defines several functions and constants to work with
Python RegEx
to work with regular expressions. To use it,
The module defines several functions and constants to work with RegEx.
re.findall
The re.findall() method returns a list of strings containing all matches
Example:
#program to extract numbers from a string
import re
string = ‘hello 12 hi 89. Howdy 34
pattern = ‘d+’
result = re.findall(patern, string)
print(result)
#Output: [‘12’, ‘89’, ‘34’]
If the pattern is no found, re.findall() returns an empty list.
re.findall()
() method returns a list of strings containing all matches
#program to extract numbers from a string
string = ‘hello 12 hi 89. Howdy 34
, string)
() returns an empty list.
re.split
The re.split method splits the string where there is a match and returns a list
of strings where the splits have occurred
Example:
import re
String = ‘Twelve:12 Eighty nine:89’
pattern = ‘d+’
result = re.split(pattern, string)
Print( result )
#Output: [ ‘Twelve:’, ‘ Eighty nine:’, ‘ . ’ ]
If the pattern is no found, re.split() returns a list containing an empty string.
re.split()
method splits the string where there is a match and returns a list
of strings where the splits have occurred.
String = ‘Twelve:12 Eighty nine:89’
(pattern, string)
#Output: [ ‘Twelve:’, ‘ Eighty nine:’, ‘ . ’ ]
() returns a list containing an empty string.
re.sub()
The syntax of re.sub() is:
re.sub(pattern, replace, string)
Example:
#program to remove all white spaces
Import re
string = ‘abc 12 de 23 n f45 6’
string = ‘abc 12 de 23 n f45 6’
#matches all whitespace characters
pattern = ‘s+’
#empty string
replace = ‘ ’
new_string = re.sub(pattern, replace, string)
If the pattern is no found, re.sub() returns the original string.
()
#program to remove all white spaces
n f45 6’
n f45 6’
#matches all whitespace characters
(pattern, replace, string)
() returns the original string.
re.subn
The re.subn() is similar to re.sub() expect it returns a tuple of 2 items
containing the new string and the number of substitutions made.
Example:
#program to remove all whitespaces
Import re
#multiline string
string = ‘abc 12 de 23 n f45 6’
# matches all whitespace characters
pattern = ‘s+’
#empty string
replace = ‘ ’
new_string = re.subn(pattern, replace, string)
print(new_string)
#Output: ( ‘abc12de23f456’, 4 )
re.subn()
() expect it returns a tuple of 2 items
containing the new string and the number of substitutions made.
#program to remove all whitespaces
n f45 6’
# matches all whitespace characters
(pattern, replace, string)
re.search
The re.search() method takes two arguments: a pattern and a string. The
method looks for the first location where the
with the string. If the search is successful,
if not, it returns None.
match = re.search(pattern, str)
re.search()
() method takes two arguments: a pattern and a string. The
method looks for the first location where the RegEx pattern produces a match
with the string. If the search is successful, re.search() returns a match object;
Example:
import re
String = “Python is fun”
#check if ‘Python’ is at the beginning
match = re.search(‘APython’, string)
if match:
if match:
print(“pattern found inside the string”)
else:
print(“pattern not found”)
#Output : pattern found inside the string
Here, match contains a match object.
#check if ‘Python’ is at the beginning
’, string)
print(“pattern found inside the string”)
#Output : pattern found inside the string
Math Object
You can get methods and attributes of a match object using
Some of the commonly used methods and attributes of match objects are
Import re
String = ‘ 39801 356, 2102 1111 ’
#Three digit number followed by space followed by two digit number
Pattern = ‘ (d{3}) (d{2}) ’
Pattern = ‘ (d{3}) (d{2}) ’
#match variable contains a Match object.
Match = re.search(pattern, string)
If match:
print(match.group())
Else:
print(“ pattern not found ”)
# Output: 801 35
Math Object
You can get methods and attributes of a match object using dir() function.
Some of the commonly used methods and attributes of match objects are:
String = ‘ 39801 356, 2102 1111 ’
#Three digit number followed by space followed by two digit number
#match variable contains a Match object.
(pattern, string)
ere, match variable contains a match object.
ur pattern (d{3}) (d{2}) has two subgroups (
art of the string of these parenthesized subgroups. Here's how:
>>> match.group(1)
‘801’
>>> match.group(2)
‘35’
‘35’
>>> match.group(3)
(‘801’, ‘35’)
>>> match.group()
(‘801’, ‘35’)
ere, match variable contains a match object.
d{2}) has two subgroups (d{3}) and (d{2}). You can get the
art of the string of these parenthesized subgroups. Here's how:
match.start(), match.end
The start() function returns the index of the matched substring. Similarly,
end() returns the end index of the matched substring.
>>> match.start()
2
>>>match.end()
8
8
The span() function returns a tuple containing start and end index of the
matched part.
>>> match.span()
(2, 8)
match.end() and match.span()
The start() function returns the index of the matched substring. Similarly,
end() returns the end index of the matched substring.
The span() function returns a tuple containing start and end index of the
match.re and
The re attribute of a matched object returns a regular expression object.
Similarly, string attribute returns the passed string.
>>> match.re
Re.compile(‘ (d{3}) (d{2}) ’)
>>> match.string
‘ 39801 356, 2102 1111 ’
match.re and match.string
The re attribute of a matched object returns a regular expression object.
Similarly, string attribute returns the passed string.
d{2}) ’)

More Related Content

Similar to regular-expression.pdf

SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)Logan Palanisamy
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expressionMegha V
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regexwayn
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionBharat17485
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracleLogan Palanisamy
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web ProgrammingAmirul Azhar
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 

Similar to regular-expression.pdf (20)

SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Arrays
ArraysArrays
Arrays
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Unitii string
Unitii stringUnitii string
Unitii string
 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 

Recently uploaded (20)

9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 

regular-expression.pdf

  • 2. What is Regular Expression A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. For example, ^a……s$ ^a……s$ The above code defines a RegEx pattern. The pattern is: letter string starting with a and ending with s. What is Regular Expression ) is a sequence of characters that defines a ^a……s$ ^a……s$ pattern. The pattern is: any five with a and ending with s.
  • 3. Python has a module named re to work with regular expression. Here’s an example: import re pattern = ‘^a…s$’ test_string = ‘abyss’ Result = re.match(pattern, test_string if result: print(“Search successful.”) else: print(“Search unsuccessful”) Here, we used re.match function to grab pattern within the The method returns a match object if the search is successful. If not, it returns to work with regular expression. Here’s an example: test_string) function to grab pattern within the test_string. The method returns a match object if the search is successful. If not, it returns No
  • 4. Specify Pattern Using RegEx: To specify regular expressions, metacharacters are used. In the above example, ^ and $ are metacharacters. Meta characters: Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's a list of metacharacters: [] . ^ $ * [] – Square brackets: Square brackets specifies a set of characters you wish to match. Square brackets specifies a set of characters you wish to match. To specify regular expressions, metacharacters are used. In the above Metacharacters are characters that are interpreted in a special way by a engine. Here's a list of metacharacters: Square brackets specifies a set of characters you wish to match. Square brackets specifies a set of characters you wish to match.
  • 5. Above, [abc] will match if the string you are trying to match contains any of the a, b or c. You can also specify a range of characters using o [a-e] is the same as [abcde]. o [1-4] is the same as [1234]. [0-39] is the same as [01239]. o o [0-39] is the same as [01239]. You can complement (invert) the character set by using caret ^ symbol at the start of a square-bracket. o [^abc] means any character except a or b or c. o [^0-9] means any non-digit character. ] will match if the string you are trying to match contains any You can also specify a range of characters using – inside square brackets. You can complement (invert) the character set by using caret ^ symbol at ] means any character except a or b or c. digit character.
  • 6. – Period: A period matches any single character (except newline A period matches any single character (except newline ’n’).
  • 7. ^ – Caret: The caret symbol ^ is used to check if a string starts with a certain character. The caret symbol ^ is used to check if a string starts with a certain character.
  • 8. $ – Dollar: The dollar symbol $ is used to check if a string ends with a certain character. The dollar symbol $ is used to check if a string ends with a certain character.
  • 9. + – Plus: The plus symbol + matches one or more occurrences of the pattern left to it The plus symbol + matches one or more occurrences of the pattern left to it.
  • 10. Python Python has a module named re to work with regular expressions. To use it, we need to import the module. import re import re The module defines several functions and constants to work with Python RegEx to work with regular expressions. To use it, The module defines several functions and constants to work with RegEx.
  • 11. re.findall The re.findall() method returns a list of strings containing all matches Example: #program to extract numbers from a string import re string = ‘hello 12 hi 89. Howdy 34 pattern = ‘d+’ result = re.findall(patern, string) print(result) #Output: [‘12’, ‘89’, ‘34’] If the pattern is no found, re.findall() returns an empty list. re.findall() () method returns a list of strings containing all matches #program to extract numbers from a string string = ‘hello 12 hi 89. Howdy 34 , string) () returns an empty list.
  • 12. re.split The re.split method splits the string where there is a match and returns a list of strings where the splits have occurred Example: import re String = ‘Twelve:12 Eighty nine:89’ pattern = ‘d+’ result = re.split(pattern, string) Print( result ) #Output: [ ‘Twelve:’, ‘ Eighty nine:’, ‘ . ’ ] If the pattern is no found, re.split() returns a list containing an empty string. re.split() method splits the string where there is a match and returns a list of strings where the splits have occurred. String = ‘Twelve:12 Eighty nine:89’ (pattern, string) #Output: [ ‘Twelve:’, ‘ Eighty nine:’, ‘ . ’ ] () returns a list containing an empty string.
  • 13. re.sub() The syntax of re.sub() is: re.sub(pattern, replace, string) Example: #program to remove all white spaces Import re string = ‘abc 12 de 23 n f45 6’ string = ‘abc 12 de 23 n f45 6’ #matches all whitespace characters pattern = ‘s+’ #empty string replace = ‘ ’ new_string = re.sub(pattern, replace, string) If the pattern is no found, re.sub() returns the original string. () #program to remove all white spaces n f45 6’ n f45 6’ #matches all whitespace characters (pattern, replace, string) () returns the original string.
  • 14. re.subn The re.subn() is similar to re.sub() expect it returns a tuple of 2 items containing the new string and the number of substitutions made. Example: #program to remove all whitespaces Import re #multiline string string = ‘abc 12 de 23 n f45 6’ # matches all whitespace characters pattern = ‘s+’ #empty string replace = ‘ ’ new_string = re.subn(pattern, replace, string) print(new_string) #Output: ( ‘abc12de23f456’, 4 ) re.subn() () expect it returns a tuple of 2 items containing the new string and the number of substitutions made. #program to remove all whitespaces n f45 6’ # matches all whitespace characters (pattern, replace, string)
  • 15. re.search The re.search() method takes two arguments: a pattern and a string. The method looks for the first location where the with the string. If the search is successful, if not, it returns None. match = re.search(pattern, str) re.search() () method takes two arguments: a pattern and a string. The method looks for the first location where the RegEx pattern produces a match with the string. If the search is successful, re.search() returns a match object;
  • 16. Example: import re String = “Python is fun” #check if ‘Python’ is at the beginning match = re.search(‘APython’, string) if match: if match: print(“pattern found inside the string”) else: print(“pattern not found”) #Output : pattern found inside the string Here, match contains a match object. #check if ‘Python’ is at the beginning ’, string) print(“pattern found inside the string”) #Output : pattern found inside the string
  • 17. Math Object You can get methods and attributes of a match object using Some of the commonly used methods and attributes of match objects are Import re String = ‘ 39801 356, 2102 1111 ’ #Three digit number followed by space followed by two digit number Pattern = ‘ (d{3}) (d{2}) ’ Pattern = ‘ (d{3}) (d{2}) ’ #match variable contains a Match object. Match = re.search(pattern, string) If match: print(match.group()) Else: print(“ pattern not found ”) # Output: 801 35 Math Object You can get methods and attributes of a match object using dir() function. Some of the commonly used methods and attributes of match objects are: String = ‘ 39801 356, 2102 1111 ’ #Three digit number followed by space followed by two digit number #match variable contains a Match object. (pattern, string)
  • 18. ere, match variable contains a match object. ur pattern (d{3}) (d{2}) has two subgroups ( art of the string of these parenthesized subgroups. Here's how: >>> match.group(1) ‘801’ >>> match.group(2) ‘35’ ‘35’ >>> match.group(3) (‘801’, ‘35’) >>> match.group() (‘801’, ‘35’) ere, match variable contains a match object. d{2}) has two subgroups (d{3}) and (d{2}). You can get the art of the string of these parenthesized subgroups. Here's how:
  • 19. match.start(), match.end The start() function returns the index of the matched substring. Similarly, end() returns the end index of the matched substring. >>> match.start() 2 >>>match.end() 8 8 The span() function returns a tuple containing start and end index of the matched part. >>> match.span() (2, 8) match.end() and match.span() The start() function returns the index of the matched substring. Similarly, end() returns the end index of the matched substring. The span() function returns a tuple containing start and end index of the
  • 20. match.re and The re attribute of a matched object returns a regular expression object. Similarly, string attribute returns the passed string. >>> match.re Re.compile(‘ (d{3}) (d{2}) ’) >>> match.string ‘ 39801 356, 2102 1111 ’ match.re and match.string The re attribute of a matched object returns a regular expression object. Similarly, string attribute returns the passed string. d{2}) ’)