0% found this document useful (0 votes)
18 views

Implementation of Regular Expression

Uploaded by

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

Implementation of Regular Expression

Uploaded by

sadiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Regular Expression

Implementation:
R E GE X IN PY T H ON

By,
Sadiya Fatima Khwaja (3178)
Overview
• Introduction
• The re module
• Implementation
Introduction
• Regular Expressions (regex) are simple expressions that can
describe the language that finite automata accept. It is the most
efficient method of representing any language.
• A regular expression can also be defined as a pattern sequence
that defines a string.
• Regular Expressions are helpful in a wide range of text processing
tasks and string processing in general, where the data does not
have to be textual.
• For example:
• Data validation,
• Data scraping (particularly web scraping),
• Simple parsing
• The creation of syntax highlighting systems and a variety of other tasks are
typical applications.
The re module in Python:

Function Description
• Regex functionality in Python
resides in a module named re.findall() finds and returns all matching
re. occurrences in a list
re.compil Regular expressions are compiled into
• How to Import re : e() pattern objects
import re re.split() Split string by the occurrences of a
character or a pattern.
• re module contains many re.sub() Replaces all occurrences of a character
functions that help us to or patter with a replacement string.
search a string for a match. re.escape Escapes special character
()
Searches for first occurrence of
re.search() character or pattern
Metacharacters Supported by the re
Module
Character(s Meaning
)
• The following table . Matches any single character
briefly summarizes all except newline
the metacharacters
supported by the re ^ • Anchors a match at the start of a
string
module. Some • Complements a character class
characters serve more
$ Anchors a match at the end of a
than one purpose: string
+ Matches one or more repetitions
\ • Escapes a metacharacter of its
special meaning
• Introduces a special character
class
• Introduces a grouping
backreference
[] Specifies a character class
Implementation: code
import re

# Define the regex pattern for a valid email address


email_pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-
Z0-9-.]+$’

# Compile the regex pattern


compiled_pattern = re.compile(email_pattern)

# Define a function to check if an email address is valid


def is_valid_email(email):
# Use the compiled pattern to check if the email
matches
return compiled_pattern.match(email) is not None
Let’s Test it:

# Test the function


test_emails = [
"example@gmail.com",
"user.name+tag+sorting@yahoo.com",
"user@invalid-domain",
"invalid-email"
]
for email in test_emails:
print(f"{email}: {'Valid’ if is_valid_email(email) else 'Invalid'}")
Output:
Explanation:
1. Define the regex pattern:
• ^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
• ^: Start of the string.
• [a-zA-Z0-9_.+-]+: One or more alphanumeric characters, dots,
underscores, pluses, or hyphens.
• +: This quantifier matches one or more of the preceding character
class. Therefore, [a-zA-Z0-9_.+-]+ matches one or more
alphanumeric characters, dots, underscores, pluses, or hyphens.
• @: This matches the literal "@" character. It separates the local part
of the email from the domain part.
• [a-zA-Z0-9-]+: One or more alphanumeric characters or hyphens.
Cont’d

• \.: This matches a literal dot. The backslash (\) is used to escape the dot
(.), as the dot is a special character in regex that matches any single
character except a newline.
• [a-zA-Z0-9-.]+: One or more alphanumeric characters, dots, or hyphens.
• $: End of the string.
2. Compile the regex pattern: Compiling the pattern can improve
performance, especially if it will be used multiple times.
3. Match the pattern: The match() method checks if the pattern matches
the entire string.
Thank you

You might also like