0% found this document useful (0 votes)
3 views6 pages

RegEx in Python

The document provides an overview of using Regular Expressions (RegEx) in Python through the built-in re module. It explains various functions such as search, findall, split, and sub, along with examples demonstrating their usage. Additionally, it covers the concept of Match Objects and wildcard searches within strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

RegEx in Python

The document provides an overview of using Regular Expressions (RegEx) in Python through the built-in re module. It explains various functions such as search, findall, split, and sub, along with examples demonstrating their usage. Additionally, it covers the concept of Match Objects and wildcard searches within strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

RegEx in Python

A RegEx, or Regular Expression, is a sequence of characters that forms a


search pattern.
RegEx can be used to check if a string contains the specified search
pattern.

RegEx Module
Python has a built-in package called re, which can be used to work with
Regular Expressions.
Import the re module:
import re
RegEx in Python
When you have imported the re module, you can start using regular
expressions:
Example
Search the string to see if it starts with "The" and ends with "Spain":

#Check if the string starts with "The" and ends with "Spain":
import re
txt = "The rain in Spain"
x = [Link]("^The.*Spain$", txt)
if x:
print("YES! We have a match!")
else:
print("No match")
output:
YES! We have a match!

Function Description

findall Returns a list containing all matches

search Returns a Match object if there is a match anywhere in the string

split Returns a list where the string has been split at each match

sub Replaces one or many matches with a string

Match Object
A Match Object is an object containing information about the search
and the result.
Note: If there is no match, the value None will be returned, instead of
the Match Object.
Example
Do a search that will return a Match Object:
import re

txt = "The rain in Spain"


x = [Link]("ai", txt)
print(x) #this will print an object
output:
<_sre.SRE_Match object; span=(5, 7), match='ai'>

The search() Function


The search() function searches the string for a match, and returns
a Match object if there is a match.
If there is more than one match, only the first occurrence of the match
will be returned:
Example
Search for the first white-space character in the string:
import re

txt = "The rain in Spain"

import re

txt = "The rain in Spain"


x = [Link](r"\s", txt)
print("The first white-space character is located in position:", [Link]())
output:
The first white-space character is located in position: 3

Search and Replace


The [Link]() function from the re module is used to replace
occurrences of a pattern in a string. It takes three main arguments:
 pattern: The regular expression pattern to search for.
 replacement: The string to replace the matched pattern with.
 string: The input string where the replacement will occur.
example:
import re

text = "The cat sat on the mat."


pattern = "cat"
replacement = "dog"
new_text = [Link](pattern, replacement, text)
print(new_text)
# Output: The dog sat on the mat.
Wildcard Search in Python
To use wildcard search in Python, the re library must be included into
the programme. A library used to work with Regular Expressions in
Python is called the re library, which is an acronym for the term Regular
Expression.
To do the search, we will compile a list of words, and after that, we will
employ the re library functions. With the aid of wildcards, we will locate
a match with a correct word.
Using the regex (regular expressions) module, we can implement
wildcards in Python.
The dot. The question mark has been replaced with a character ?.
1. import re
2. # To change the outcomes, add or remove terms from this list.
3. words = ["color", "colour", "work", "working", "apple", "master"
, "driving"]
4. for i in words:
5. # Instead of the? symbol, use the . symbol
6. if [Link]('c.l', i) :
7. print (i)
Output:
Color
Colour

FindAll:
The [Link]() method in Python's re module is used to find all
occurrences of a specified pattern within a string. It returns a list
containing all non-overlapping matches as strings. If one or more
groups are present in the pattern, it returns a list of groups. An empty
list is returned if the pattern is not found.
Code:
import re

text = "The cat sat on the mat, the fat cat."


pattern = "cat"
result = [Link](pattern, text)
print(result)
# Output: ['cat', 'cat']
Split:
The [Link]() method in Python's re module is used to split a string into
a list of substrings based on a regular expression pattern.
Python
import re

text = "apple,banana;orange|grape"
pattern = "[,;|]" # Matches comma, semicolon, or pipe
result = [Link](pattern, text)
print(result)
# Output: ['apple', 'banana', 'orange', 'grape']

You might also like