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

Python Assignment Unit5 Answers

The document covers Python's regular expressions and HTML information retrieval. It explains the usage of the re module for string manipulation and provides examples of regex patterns and functions. Additionally, it details how to retrieve information from HTML files using urllib and HTMLParser, along with various regex programs for string searching and data extraction.

Uploaded by

zooquest007
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)
7 views5 pages

Python Assignment Unit5 Answers

The document covers Python's regular expressions and HTML information retrieval. It explains the usage of the re module for string manipulation and provides examples of regex patterns and functions. Additionally, it details how to retrieve information from HTML files using urllib and HTMLParser, along with various regex programs for string searching and data extraction.

Uploaded by

zooquest007
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

Python Assignment – Unit 5

Regular Expressions and HTML Information Retrieval

Q1. Regular Expressions and their Usage


A regular expression (regex) is a special sequence of characters that defines a search pattern. Python
provides the re module to work with regular expressions. They are widely used for string searching,
matching, splitting, and substitution.

Key Functions in Python's re Module:


• [Link](pattern, string) – Searches for a pattern anywhere in the string.
• [Link](pattern, string) – Matches the pattern only at the beginning.
• [Link](pattern, string) – Returns a list of all non-overlapping matches.
• [Link](pattern, repl, string) – Replaces matched pattern with a new string.
• [Link](pattern, string) – Splits the string at every match.
• [Link](pattern) – Compiles a pattern into a regex object for reuse.

Common Metacharacters:
• . (dot) – Matches any character except newline.
• ^ – Matches beginning of string.
• $ – Matches end of string.
• * – Matches 0 or more repetitions.
• + – Matches 1 or more repetitions.
• ? – Matches 0 or 1 repetition.
• {m,n} – Matches between m and n repetitions.
• [abc] – Matches any of the characters in the set.
• \d – Matches any digit (0-9).
• \w – Matches any alphanumeric character.
• \s – Matches any whitespace.

Example:
import re

text = 'Hello, my email is test@[Link] and phone is 9876543210'

# Search for email


email = [Link](r'[\w.-]+@[\w.-]+\.\w+', text)
print('Email:', [Link]()) # Output: test@[Link]

# Find all digits


digits = [Link](r'\d+', text)
print('Digits:', digits) # Output: ['9876543210']
Q2. Retrieving Information from an HTML File
Python's urllib module is used to fetch web pages, and the [Link] (or BeautifulSoup) is used to
parse HTML and extract useful data. Key steps:
• 1. Import [Link] and [Link].
• 2. Use [Link]() to open a URL or read an HTML file.
• 3. Create a class inheriting from HTMLParser and override handle_starttag, handle_data,
handle_endtag.
• 4. Call feed() method to parse HTML content.
• 5. Extract desired data such as links, text, or attributes.

Program to Retrieve Information from an HTML File:


from [Link] import HTMLParser
import [Link]

class MyHTMLParser(HTMLParser):
def __init__(self):
super().__init__()
[Link] = []
self.data_list = []

def handle_starttag(self, tag, attrs):


print(f'Start tag: <{tag}>')
if tag == 'a':
for attr in attrs:
if attr[0] == 'href':
[Link](attr[1])

def handle_endtag(self, tag):


print(f'End tag: </{tag}>')

def handle_data(self, data):


data = [Link]()
if data:
self.data_list.append(data)
print(f'Data: {data}')

# Read local HTML file


with open('[Link]', 'r') as f:
html_content = [Link]()

parser = MyHTMLParser()
[Link](html_content)

print('\nAll Links Found:')


for link in [Link]:
print(link)

print('\nAll Text Data Found:')


for item in parser.data_list:
print(item)
Q3. Regular Expression Programs

(a) Search for string starting with 'm' having exactly 3 characters
import re

text = 'man men mat cat bat map mo me milk'


pattern = r'\bm[a-z]{2}\b'

# Using search()
result = [Link](pattern, text)
print('search():', [Link]() if result else 'Not found')
# Output: man

# Using findall()
result = [Link](pattern, text)
print('findall():', result)
# Output: ['man', 'men', 'mat', 'map']

# Using map()
words = [Link]()
result = list(filter(lambda w: [Link](r'm[a-z]{2}', w), words))
print('map/filter():', result)
# Output: ['man', 'men', 'mat', 'map']

(b) Split a string at non-alphanumeric characters


import re

text = 'Hello, World! How are you? I am fine.'

# Split at one or more non-alphanumeric characters


result = [Link](r'[^a-zA-Z0-9]+', text)
print('Split result:', result)
# Output: ['Hello', 'World', 'How', 'are', 'you', 'I', 'am', 'fine', '']

# Remove empty strings


result = [w for w in result if w]
print('Cleaned:', result)

(c) Replace a string with a new string


import re

text = 'The cat sat on the mat. The cat is fat.'

# Replace 'cat' with 'dog'


new_text = [Link](r'cat', 'dog', text)
print('After replacement:', new_text)
# Output: The dog sat on the mat. The dog is fat.

# Replace only first occurrence


new_text2 = [Link](r'cat', 'dog', text, count=1)
print('First only:', new_text2)
# Output: The dog sat on the mat. The cat is fat.

(d) Retrieve all words of 3, 4, or 5 characters length


import re

text = 'I love Python programming it is fun and very easy to learn'

# Match words of exactly 3, 4, or 5 characters


result = [Link](r'\b[a-zA-Z]{3,5}\b', text)
print('Words with 3-5 chars:', result)
# Output: ['love', 'is', 'fun', 'and', 'very', 'easy', 'learn']

# Separate by length
for length in [3, 4, 5]:
words = [Link](rf'\b[a-zA-Z]{{{length}}}\b', text)
print(f'{length}-letter words: {words}')

(e) Retrieve dates of birth from a string


import re

text = '''
Name: Rahul, DOB: 15/08/2000
Name: Priya, DOB: 22-11-1998
Name: Amit, DOB: 2001-03-07
Name: Sneha, DOB: 10/05/1995
'''

# Pattern to match DD/MM/YYYY, DD-MM-YYYY, or YYYY-MM-DD


pattern = r'\b(\d{2}[/-]\d{2}[/-]\d{4}|\d{4}-\d{2}-\d{2})\b'
dates = [Link](pattern, text)
print('Dates of Birth found:')
for d in dates:
print(d)
# Output:
# 15/08/2000
# 22-11-1998
# 2001-03-07
# 10/05/1995

(f) Read email IDs from a file


import re

# Assume '[Link]' contains text with email addresses


# Sample content in [Link]:
# Contact us at info@[Link] or support@[Link]
# Sales: sales@[Link], returns: returns@[Link]

try:
with open('[Link]', 'r') as f:
content = [Link]()

pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
emails = [Link](pattern, content)

print('Email IDs found in file:')


for email in emails:
print(email)
except FileNotFoundError:
print('File not found. Please provide [Link]')

(g) Retrieve data from a file and write to another (Employee No., Salary)
import re

# Assume [Link] contains lines like:


# E001 John Doe 45000
# E002 Jane Smith 62000
# E003 Bob Kumar 38000

try:
with open('[Link]', 'r') as infile:
content = [Link]()

# Pattern to match Employee No. and Salary


pattern = r'(E\d{3})\s+[A-Za-z ]+?\s+(\d{4,6})'
matches = [Link](pattern, content)

with open('[Link]', 'w') as outfile:


[Link]('Employee No.\tSalary\n')
[Link]('-' * 25 + '\n')
for emp_no, salary in matches:
line = f'{emp_no}\t\t{salary}\n'
[Link](line)
print(line, end='')

print('\nData written to [Link] successfully!')


except FileNotFoundError:
print('Input file not found.')

You might also like