0% found this document useful (0 votes)
12 views4 pages

TOA Assignment 1

Uploaded by

Rooh ul Hasnain
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)
12 views4 pages

TOA Assignment 1

Uploaded by

Rooh ul Hasnain
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
You are on page 1/ 4

RegEx Program in Python

ASSIGNMENT SUBMITTED TO
Dr. Aftab A Shaikh

Course
Theory of Automata
Spring-2023

Prepared By:
Asad Ali (MSC-21F-001)
Asad Ali (MSC-21F-001) 2023

Code#1:
import re return processed_name
def process_name(name): else:
# Regular expression pattern to match last # No match found, return the original name
name, first name, and optional suffix return name
# Test cases
pattern = r'^(\w+(?: \w+)*),\s*([\w. names = [
]+)\s*(\w*\.*\s*)$' "Dijkstra, Edsger W.",
# Extract the components of the name using "Hoare, C. Anthony R..",
the pattern "Peterson 3rd, Gordon E.",
match = re.match(pattern, name) "Key Point Software",
if match: "Evergreen Valley College"
last_name = match.group(1) ]
first_name = match.group(2) for name in names:
suffix = match.group(3) processed_name = process_name(name)
# Construct the "first-name-first" format print(processed_name)
processed_name = f"{first_name.strip()}
{last_name}"
Asad Ali (MSC-21F-001) 2023

Output-1:

Code#2
import re
def process_phone(phone):
phone = re.sub(r'^(\d{3})-(\d{4})$', r'(0300) \1-\2', phone)
phone = re.sub(r'^(\d{3})-(\d{3})-(\d{4})$', r'(0333) \2-\3', phone)
phone = re.sub(r'^\((\d{3})\) (\d{3})-(\d{4})$', r'(0300) \2-\3', phone)
phone = re.sub(r'^(\d{3})\s(\d{3})-(\d{4})$', r'(0\1) \2-\3', phone)
return phone
phone_numbers = [
'274-7900',
'510-555-0297',
'312 555-8763',
'(800) 775-7731'
]
for phone in phone_numbers:
processed_phone = process_phone(phone)
print(processed_phone)

Code#3:
import re
def process_name(name):
# Check if the name contains a comma
if ',' in name:
# Split the name into last name and first name
last_name, first_name = re.split(r',\s*', name)
# Reorder the names in "first-name-first" form
return f"{first_name} {last_name}"
else:
return name
def process_phone(phone):
Asad Ali (MSC-21F-001) 2023

# Remove any non-digit characters from the phone number


digits = re.sub(r'\D', '', phone)
# Format the phone number as (NNNN) NNN-NNNN
formatted_phone = f"({digits[:4]}) {digits[4:7]}-{digits[7:]}"
return formatted_phone
# Define the data with names and phone numbers
data = [
('Gordon E. Peterson 3rd', '0300 555-1212'),
('C. Anthony R. Hoare Key Point Software', '0312 555-8763'),
('Key Point Software', '0300 249-6625'),
('Evergreen Valley College', '0308 274-7900'),
('Edsger W. Dijsktra', '0310 555-0297'),
("O'Reilly & Associates", '0310 775-7731')
]
# Process names and phones using the respective functions
processed_data = [(process_phone(phone), process_name(name)) for name, phone in data]
# Sort the data by phone number
sorted_data = sorted(processed_data, key=lambda x: x[0])
# Print the sorted data
print("Phone\t\t\tName")
for phone, name in sorted_data:
print(f"{phone}\t{name}")

Output-3

You might also like