0% found this document useful (0 votes)
7 views11 pages

Understanding Regular Expressions (RegEx)

Regular Expressions (RegEx) are a powerful tool for searching, matching, replacing, and manipulating text by describing patterns in strings. Common use cases include validating emails, extracting data, and replacing text, with functions like findall(), sub(), search(), match(), compile(), and split() providing various functionalities. Match objects returned by functions like search() and match() allow for retrieving matched text and its position in the string.

Uploaded by

Atharva Kale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Understanding Regular Expressions (RegEx)

Regular Expressions (RegEx) are a powerful tool for searching, matching, replacing, and manipulating text by describing patterns in strings. Common use cases include validating emails, extracting data, and replacing text, with functions like findall(), sub(), search(), match(), compile(), and split() providing various functionalities. Match objects returned by functions like search() and match() allow for retrieving matched text and its position in the string.

Uploaded by

Atharva Kale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Regular Expressions (RegEx):

are a powerful tool for working with text. They provide a way to describe patterns in strings, allowing you to
search, match, replace, and manipulate text in complex and flexible ways.

Think of RegEx as a pattern language for describing text. It helps you specify exactly what you're looking for in a
string, from simple things like finding a word, to more complex tasks like validating an email or extracting
specific data.

Why use RegEx?


1. Search and match : Find occurrences of patterns in text (e.g., finding all email addresses in a document).
2. Text validation : Check if a string matches a specific format (e.g., validate phone numbers, emails, or zip
codes).
3. Replace text : Replace parts of the string that match a pattern with something else (e.g., replacing all spaces
with underscores).
4. Text extraction: Extract parts of a string that match a pattern (e.g., extracting dates from text).
5. Split text : Split a string into multiple parts based on a pattern (e.g., split a sentence into words).

Special Sequences :

These are shorthand notations for common sets of characters.


Examples:
\d — Any digit (equivalent to [0-9] ).
\w — Any alphanumeric character (equivalent to [a-zA-Z0-9_] ).
\s — Any whitespace character (spaces, tabs, newlines).

Basic Example
Suppose you want to find all phone numbers in the format 123-456-7890 . You could use the following RegEx
pattern:

regex
\d{3}-\d{3}-\d{4}

\d matches any digit (0–9).


{3} means exactly three of those digits.
- matches the dash between the number groups.

Common RegEx Use Cases


1. Validating an Email:

RegEx can be used to ensure an email follows the correct format.


Example: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
2. Extracting Dates:

RegEx can match patterns like dates, such as DD/MM/YYYY or MM-DD-YYYY .


3. Password Validation :

You can use RegEx to enforce rules like:


At least one uppercase letter.
At least one number.
At least 8 characters long.
4. Replacing Text :
Example: Remove all non-alphanumeric characters from a string.

1. findall()
Description:
The findall() function finds all occurrences of a pattern in the string. It returns a list of strings (or an
empty list if no match is found). Unlike search() or match() , it doesn’t stop after finding the first match,
it keeps searching until the entire string has been scanned.
Usage:
It matches anywhere in the string.
Example:

In [1]:
import re

pattern = r"\d+" # Find all numbers


text = "The price is 100 dollars, and 250 yen."

matches = [Link](pattern, text)


print(matches)

['100', '250']

2. sub()
Description:
The sub() function is used for substitution. It replaces the occurrences of the pattern with a given
replacement string.
Usage:
You can use it to replace one or more occurrences of a pattern in the string.
Example:

In [33]:
import re

pattern = r"apple"
text = "I like apple pie and apple juice."

# Replace 'apple' with 'orange'


result = [Link](pattern, "orange", text)
print(result)

I like orange pie and orange juice.

3. search()
Description:
The search() function scans through the string and returns the first match of the pattern. It returns a
match object if a match is found, or None if no match is found.
Usage:
It matches anywhere in the string.
Example:

In [36]:
import re
pattern = r"apple"
text = "I like apple pie."

match = [Link](pattern, text)


if match:
print("Match found:", [Link]())
else:
print("No match found.")

Match found: apple

4. match()
Description:
The match() function checks if the pattern matches at the beginning of the string. It returns a match
object if it matches the start of the string, or None if it doesn’t.
Usage:
It only matches at the start of the string.
Example:

In [40]:
import re

pattern = r"apple"
text = "apple pie is delicious."

match = [Link](pattern, text)


if match:
print("Match found:", [Link]())
else:
print("No match found.")

Match found: apple

5. compile()
Description:
The compile() function compiles a regular expression pattern into a regex object . This can be helpful
when you need to use the same pattern multiple times, as it speeds up the matching process.
Usage:
You can compile the pattern once and use it with other functions like match() , search() , etc.
Example:

In [5]:
import re

# Compile the pattern


pattern = [Link](r"\d+")

text = "The number is 12345."


match = [Link](text)

if match:
print("Match found:", [Link]())
else:
print("No match found.")

Match found: 12345

6. split()
Description:
The split() function splits the string at every occurrence of the pattern. It returns a list of substrings. The
pattern can be any regular expression, and it will split the string based on it.
Usage:
This method divides the string into substrings wherever the pattern is matched.
Example:

In [6]:
import re

pattern = r"\s+" # Split by one or more spaces


text = "This is a sentence."

words = [Link](pattern, text)


print(words)

['This', 'is', 'a', 'sentence.']

Summary Table:
Method Description Example Usage

Finds all occurrences of the pattern. Returns a


findall() [Link](r"\d+", "123 abc 456")
list.

sub() Substitutes parts of the string with a new value. [Link](r"apple", "orange", text)

search() Finds the first match anywhere in the string. [Link](r"apple", text)

match() Matches the pattern only at the start of the string. [Link](r"apple", text)

compile() Compiles the pattern for faster repeated use. [Link](r"\d+")

split() Splits the string at occurrences of the pattern. [Link](r"\s+", text)

Match object in Python and explore the functions that you can use when working with
it.
A Match object is returned by functions like [Link]() or [Link]() , and it provides various methods
to retrieve information about the matched text.

Functions Offered by a Match Object


1. group()

Description:
The group() method returns the string that was matched by the regular expression.
Usage:
group() returns the entire matched string.
group(n) returns the substring that matched the n th parenthesized sub-pattern in the regex
(parentheses are used for grouping in RegEx).
Examples:

In [12]:
import re

# Define the pattern


pattern = r"(\d{3})-(\d{3})-(\d{4})" # Phone number format
# The text to search
text = "My phone number is 123-456-7890."

# Search for the pattern


match = [Link](pattern, text)

if match:
print([Link]()) # Output: '123-456-7890' (whole match)
print([Link](1)) # Output: '123' (1st group)
print([Link](2)) # Output: '456' (2nd group)
print([Link](3)) # Output: '7890' (3rd group)
print([Link](1, 3)) # Output: ('123', '7890') (1st and 3rd groups as tuple
)
123-456-7890
123
456
7890
('123', '7890')

1. start()

Description:
The start() method returns the starting position of the match in the string (the index of where the
match begins).
Example:

In [14]:
match = [Link](r"(\d{3})-(\d{3})-(\d{4})", "My phone number is 123-456-7890.")

if match:
print([Link]()) # Output: 19 (position where '123-456-7890' starts)
19

1. end()

Description:
The end() method returns the ending position of the match (i.e., the position just after the last
character of the match).
Example:

In [15]:
match = [Link](r"(\d{3})-(\d{3})-(\d{4})", "My phone number is 123-456-7890.")

if match:
print([Link]()) # Output: 39 (position just after the last character of the ma
tch)
31

1. span()

Description:
The span() method returns a tuple with two elements: the start and end positions of the match.
Example:

In [16]:

match = [Link](r"(\d{3})-(\d{3})-(\d{4})", "My phone number is 123-456-7890.")

if match:
print([Link]()) # Output: (19, 39) (start and end positions of the match)
(19, 31)

Summary of Methods for Match Object:


Method Description Example

group() Returns the string that matched. [Link]()

group(1) Returns the substring that matched the 1st sub-pattern. [Link](1)

start() Returns the starting position of the match. [Link]()

end() Returns the ending position of the match. [Link]()

span() Returns a tuple (start, end) of the match positions. [Link]()

Example:
Here’s a complete example combining all the methods we’ve discussed:

import re

# Pattern to match phone number


pattern = r"(\d{3})-(\d{3})-(\d{4})"
text = "Call me at 123-456-7890 or 987-654-3210."

# Search for the first match


match = [Link](pattern, text)

if match:
print(f"Whole match: {[Link]()}") # '123-456-7890'
print(f"First part: {[Link](1)}") # '123'
print(f"Second part: {[Link](2)}") # '456'
print(f"Third part: {[Link](3)}") # '7890'
print(f"Start position: {[Link]()}") # 13
print(f"End position: {[Link]()}") # 26
print(f"Match span: {[Link]()}") # (13, 26)

When to Use Match Object Methods


group() is useful when you want to retrieve specific parts of the matched string, especially when you use
parentheses in your RegEx pattern to create groups.
start() and end() are helpful when you need to know the position of the match in the text (e.g., for
highlighting or replacing).
span() is a convenient way to get both the start and end positions together, especially when you need to
work with those positions.

Examples:
In [32]:
import re

# Sample text containing mobile numbers, email addresses, dates, and Aadhaar card numbers
text = """My Mobile Number is : 9988776655,
email id is : viratkohli@[Link]
todays date is : 30-05-2022
date=13/12/2023
4444 5555 6666
atharvh@[Link]
pranjal_123@[Link]
Mobile Number is : 7788776648
Mobile Number is : 77887766
"""

# Find all 10-digit mobile numbers using a regex pattern that matches exactly 10 digits
mob = [Link]("[0-9]{10}", text)
print(mob) # Output: ['9988776655', '7788776648']

# Using a special character \d for digits, the pattern \d{10} also matches 10 digits
mob1 = [Link]("\d{10}", text)
print(mob1) # Output: ['9988776655', '7788776648']

# Find dates in the format 'DD-MM-YYYY' or 'DD/MM/YYYY'


# [0-9]{2} matches two digits, [-/] matches either a hyphen or a slash, [0-9]{4} matches
four digits
date = [Link]("[0-9]{2}[-/][0-9]{2}[-/][0-9]{4}", text)
print(date) # Output: ['30-05-2022', '13/12/2023']

# Using the special character \d to match digits, [-/] matches the separator (hyphen or s
lash)
date1 = [Link]("\d{2}[/-]\d{2}[/-]\d{4}", text)
print(date1) # Output: ['30-05-2022', '13/12/2023']

# Find email addresses following the pattern (letters before '@', domain, and TLD)
# [a-z]{2,20} matches 2 to 20 lowercase letters, [@] matches the "@" symbol, [a-z]{5} mat
ches 5 or more lowercase letters, [.] matches the dot, and [a-z.]{2,20} matches the TLD (
e.g., .com, .[Link])
email = [Link]("[a-z]{2,20}[@][a-z]{5}[.][a-z.]{2,20}", text)
print(email) # Output: ['viratkohli@[Link]', 'atharvh@[Link]', 'pranjal_123@gmai
[Link]']

# Using the special character \w for word characters (letters, digits, and underscores)
email_w = [Link]("\w{2,20}[@]\w{5}[.]\w{2,20}", text)
print(email_w) # Output: ['viratkohli@[Link]', 'atharvh@[Link]', 'pranjal_123@gm
[Link]']

# Find Aadhaar card numbers in the format '#### #### ####' (4 digits followed by a space,
repeated 3 times)
aadarecard1 = [Link]("[0-9]{4}[ ][0-9]{4}[ ][0-9]{4}", text)
print(aadarecard1) # Output: ['4444 5555 6666']

# Using \d for digits and \s for spaces, the same pattern can be written as \d{4}\s\d{4}\
s\d{4}
aadarecard2 = [Link]("\d{4}\s\d{4}\s\d{4}", text)
print(aadarecard2) # Output: ['4444 5555 6666']

['9988776655', '7788776648']
['9988776655', '7788776648']
['30-05-2022', '13/12/2023']
['30-05-2022', '13/12/2023']
['viratkohli@[Link]', 'atharvh@[Link]']
['viratkohli@[Link]', 'atharvh@[Link]', 'pranjal_123@[Link]']
['4444 5555 6666']
['4444 5555 6666']

Explanation of Each Line:


1. Mobile Numbers ( mob & mob1 ):

The pattern [0-9]{10} or \d{10} matches any sequence of exactly 10 digits.


findall() returns a list of all 10-digit numbers it finds in the text.
2. Dates ( date & date1 ):

The pattern [0-9]{2}[-/][0-9]{2}[-/][0-9]{4} or \d{2}[/-]\d{2}[/-]\d{4} is used to find


dates in formats like 30-05-2022 or 13/12/2023 .
[0-9]{2} matches two digits for the day or month, and [0-9]{4} matches four digits for the year.
[-/] matches either a hyphen or a slash as the separator between the date, month, and year.
3. Emails ( email & email_w ):
3. Emails ( email & email_w ):

The pattern [a-z]{2,20}[@][a-z]{5}[.][a-z.]{2,20} or \w{2,20}[@]\w{5}[.]\w{2,20} is


designed to match email addresses.
[a-z]{2,20} matches the username part with 2 to 20 lowercase letters.
[@] matches the "@" symbol.
[a-z]{5} matches the domain name part with 5 or more lowercase letters.
[.] matches the period before the top-level domain (TLD).
[a-z.]{2,20} matches the TLD, which can contain letters and a dot.
4. Aadhaar Numbers ( aadarecard1 & aadarecard2 ):

The pattern [0-9]{4}[ ][0-9]{4}[ ][0-9]{4} or \d{4}\s\d{4}\s\d{4} is used to find Aadhaar


card numbers in the format 4444 5555 6666 .
[0-9]{4} matches four digits, and [ ] matches the space separator.
\s is used for matching spaces in the second version of the pattern ( aadarecard2 ).

Notes:
Special Characters:
\d is a shorthand for any digit (equivalent to [0-9] ).
\w matches any "word" character, which includes letters, digits, and underscores.
\s matches any whitespace character (space, tab, newline).
Quantifiers:
{n} specifies exactly n occurrences of the preceding element.
{n, m} specifies a range, matching at least n occurrences, but no more than m occurrences.

In [18]:
mob=[Link]("\d{10}",text)
print(mob)

['9988776655', '7788776648']

In [19]:
aadhar=[Link]("[0-9]{4}[ ][0-9]{4}[ ][0-9]{4}",text)
print(aadhar)

['4444 5555 6666']

In [20]:
email=[Link]("[a-z]{2,20}[@][a-z.]{2,20}",text)
email
Out[20]:
['viratkohli@[Link]', 'atharvh@[Link]']

In [23]:
text="""
pranjal_123@[Link]

"""

In [24]:
email=[Link]("[a-z]{2,20}[@][a-z.]{2,20}",text)
email
email1=[Link]("[a-z0-9]{2,20}[@][a-z.]{2,20}",text)
email1=[Link]("\w{2,20}[@][a-z.]{2,20}",text)
email1
Out[24]:
Out[24]:
['pranjal_123@[Link]']

In [25]:
import re
text = """My Mobile Number is : 9988776655,
email id is : viratkohli@[Link]
todays date is : 30-05-2022

Mobile Number is : 7788776648


29-05-2021
31-05-2012
30-09-2022
"""
date=[Link]("[0-9]{2}[-][0-9]{2}[-][0-9]{4}",text)
date
Out[25]:
['30-05-2022', '29-05-2021', '31-05-2012', '30-09-2022']

In [26]:
import re
text = """My Mobile Number is : 9988776655,
email id is : viratkohli@[Link]
todays date is : 30-05-2022

Mobile Number is : 7788776648


29-05-2021
31-05-2012
30-09-2022
30/09/2022
30.09.2022
1/12/23
"""
date=[Link]("[0-9]{1,5}[-/.][0-9]{1,2}[-/.][0-9]{1,4}",text)
date
Out[26]:
['30-05-2022',
'29-05-2021',
'31-05-2012',
'30-09-2022',
'30/09/2022',
'30.09.2022',
'1/12/23']

In [27]:
text=""""PAN CARD NUMBER : ASDFG4567P
PAN CARD NUMBER : POIUY3456W
PAN CARD NUMBER : POIUY34567
"""
pan=[Link]("[A-Z]{5}[0-9]{4}[A-Z]",text)
print(pan)
pan1=[Link]("\D{5}\d{4}\D",text)
print(pan1)

['ASDFG4567P', 'POIUY3456W']
['ASDFG4567P', 'POIUY3456W']

In [28]:
import re
text = """My Mobile Number is : 9988776655,
email id is : viratkohli@[Link]
email id is : viratkohli123@[Link]

"""
email=[Link]("[a-z0-9]{2,20}[@][a-z.]{2,20}",text)
email
Out[28]:
['viratkohli@[Link]', 'viratkohli123@[Link]']

In [29]:
text="""email id is : viratkohli@[Link]
email id is : viratkohli123@[Link]
email id is : virat_kohli123@[Link]
email id is : virat-kohli@[Link]
email id is : virat-kohli@[Link]
email id is : [Link]@[Link]
"""
email=[Link]("[a-z0-9-_.]{2,20}[@][a-z.]{2,20}",text)
email
Out[29]:
['viratkohli@[Link]',
'viratkohli123@[Link]',
'virat_kohli123@[Link]',
'virat-kohli@[Link]',
'virat-kohli@[Link]',
'[Link]@[Link]']

Exercise:
text =Name: Vishal Vijay Tandel. PAN Number: AXCPT8626T Mobile Number : 8412835885 DOB: 17/11/1996 Email
id: vishaltandel19@[Link] aadhar number = 3456 1232 9876

print(pan_number

print(mob_number)

print(date)

print(email_id)

print(aadhar_number)

SPECIAL CHARACTERS:-
1) \d:-Returns a match where string contains digit (0-9).
2) \D:-Returns a match where string does not contains [Link] will contain spaces,special characters,letters etc
3) \w:-Returns a match where string contains any alphanumeric character and underscore i.e. [0-9a-z_]\w{2,20}
4) \W:-Returns a match where string does not contains any alphanumeric character and underscore i.e. [^0-9a-
z_]

5) \s:-Returns a match where string contains a white space [Link] default occurance is 1. for two
spaces-\s[2]

6) /s:-Returns a match where string does not contains space.

7) \b:-Returns a match where characters are at begining or at end of word i.e. perfect match

BY Pranjal Gajbhiye(AIE)

Happy Learning...
In [ ]:

You might also like