# Computer Engineering-III- Python Programming(SBL)
# Practical No 7 : To develop/write simple Python program to extract data using regular expxression store
in files.
# Student Name & roll number : STAVAN KALKUMBE / 1023146
# Date of Perfoming:
import re
def read_file(file_name):
with open(file_name, "r") as file:
content = file.read()
return content
def find_pattern(content, pattern):
matches = re.findall(pattern, content)
return matches
def validate_email(content):
pattern = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
matches = re.findall(pattern, content)
return matches
def validate_phone(content):
pattern = r"\d{10}"
matches = re.findall(pattern, content)
return matches
def main():
HELLO = "HELLO.txt"
content = read_file(HELLO)
if content:
print("File Content:")
print(content)
pattern = r"Stavan"
matches = find_pattern(content, pattern)
print(f"Matches for '{pattern}': {matches}")
email_matches = validate_email(content)
if email_matches:
print("Emails found:")
for email in email_matches:
print(email)
else:
print("No emails found.")
phone_matches = validate_phone(content)
if phone_matches:
print("Phone_no found:")
for phone in phone_matches:
print(phone)
else:
print("No phone_no found.")
if __name__ == "__main__":
main()
# Laboratory Exercise 2: Validate a Phone Number
# Write a Python regex pattern that validates a phone number in the format (XXX) XXX-XXXX or XXX-XXX-
XXXX.
import re
pattern1 = "\d{3}-\d{3}-\d{4}"
pattern2 = "\(\d{3}\) \d{3}-\d{4}"
phone_no=input("Enter phone number:")
var1=bool(re.match(pattern1, phone_no))
var2=bool(re.match(pattern2, phone_no))
if var1 or var2:
print(phone_no)
print("Valid phone no")
else:
print(phone_no)
print("Invalid phone no")
>> Output
(123) 456-7891
Valid phone no
# Laboratory Exercise 3: Extract URLs from Text
# Write a regular expression to extract all URLs from a given text.
import re
text = "Visit https://www.google.com or http://example.com for more information."
#Your code here
pattern = r"(https?:\/\/[\da-z\.-]+\.com)"
matching = re.findall(pattern, text)
print(matching)
>>Output
['https://www.google.com', 'http://example.com']
#Laboratory Exercise 4: Password Validation
# Write a Python regex to check whether a given password is valid:
# 1.Must contain at least 8 characters
# 2.Should include both lowercase and uppercase characters
# 3.Should contain at least one number
import re
try:
password = input("Enter password:")
pattern = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$"
va1 = bool(re.fullmatch(pattern, password))
if va1:
print("Valid password")
else:
print("Invalid.")
except Exception as e:
print("An error occurred:", str(e))
>>Output
Valid password