You are on page 1of 18

Python Assingment 3

Regular Expressions
Shashank Pratap
22MCA1061

1.Write a Python program to check that a string contains only a certain set of characters (a-z, A-Z
and 0-9) using regular expression

Solution:

#1

import re

def check_string_characters(string):

pattern = r'^[a-zA-Z0-9]+$'

if re.match(pattern, string):

print("String contains only a-z, A-Z, and 0-9 characters")

else:

print("String contains other characters besides a-z, A-Z, and 0-9")

# Testing the function

input_string = input("Enter a string: ")

check_string_characters(input_string)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:
2. Write a Python program that matches a string that has an ‘a’ followed by

a. Zero or more 'b's. For example, a, ab, abb, abbb, ....

b. One or more ‘b’s. For example ab, abbc, abbbc

c. Two to three ‘b’s. For example abb , abbb

Solution:

#2

import re

def match_patterns(string):

pattern_a = r'^a(b*)$'

pattern_b = r'^ab+$'

pattern_c = r'^ab{2,3}$'
if re.match(pattern_a, string):

print("Pattern a matched:", string)

if re.match(pattern_b, string):

print("Pattern b matched:", string)

if re.match(pattern_c, string):

print("Pattern c matched:", string)

# Testing the function

input_string = input("Enter a string: ")

match_patterns(input_string)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:

3. Write a Python program that matches a word containing 'z' in the string “The quick

brown fox jumps over the lazy dog”.


Solution:

#3

import re

def match_word_with_z(string):

pattern = r'\b\w*z\w*\b'

matches = re.findall(pattern, string)

if matches:

print("Words containing 'z':", matches)

else:

print("No words containing 'z' found")

# Testing the function

input_string = "The quick brown fox jumps over the lazy dog"

match_word_with_z(input_string)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:
4. Write a Python program to search the numbers (0-9) of length between 1 to 3 in a given string
“Exercises number 1, 12, 13, and 345 are important"

Solution:

import re

def match_word_with_z(string):

pattern = r'\b\w*z\w*\b'

matches = re.findall(pattern, string)

if matches:

print("Words containing 'z':", matches)

else:

print("No words containing 'z' found")

# Testing the function

input_string = "The quick brown fox jumps over the lazy dog"

match_word_with_z(input_string)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:
5. Write a Python program to find the substring “exercises” within a string “Python exercises, PHP
exercises, C# exercises”

Solution:

def find_substring(string, substring):

index = string.find(substring)

if index != -1:

print("Substring found at index:", index)

else:

print("Substring not found")

# Testing the function

input_string = "Python exercises, PHP exercises, C# exercises"

substring = "exercises"

find_substring(input_string, substring)

print("\n\nShashank Pratap 22MCA1061")


Screenshot:

6. Create your own text file. The text file should have multiple lines of text that should

contain the word python.

a. Use regular expressions to count the number of times the word “python”

appears in the file?

b. With the help of regular expressions, search for lines that contain the word

“python”?

c. Replace a specific word or phrase with another word or phrase in a text file

using regular expressions? For example, instead of “python” use “java”

Solution:

import re

# Task a: Count the number of times the word "python" appears in the file

def count_python_occurrences(file_name):

with open(file_name, 'r') as file:


contents = file.read()

pattern = r'python'

matches = re.findall(pattern, contents, re.IGNORECASE)

count = len(matches)

print(f"The word 'python' appears {count} times in the file.")

# Task b: Search for lines that contain the word "python"

def search_lines_with_python(file_name):

with open(file_name, 'r') as file:

pattern = r'^.*python.*$'

for line in file:

if re.search(pattern, line, re.IGNORECASE):

print(line.rstrip())

# Task c: Replace a specific word or phrase with another word or phrase in a text file

def replace_word_in_file(file_name, old_word, new_word):

with open(file_name, 'r') as file:

contents = file.read()

modified_contents = re.sub(old_word, new_word, contents, flags=re.IGNORECASE)

with open(file_name, 'w') as file:

file.write(modified_contents)

print(f"Word '{old_word}' replaced with '{new_word}' in the file.")

# Creating a sample text file

file_name = 'sample_file.txt'

with open(file_name, 'w') as file:

file.write("This is a sample file.\n")

file.write("It contains multiple lines.\n")

file.write("Some lines may have the word 'python' in them.\n")

file.write("Python is a popular programming language.\n")


file.write("This line does not contain 'python'.\n")

file.write("The word 'python' can be written in different ways, like Python, PYTHON,
pythonic, etc.\n")

# Task a: Count the number of times the word "python" appears in the file

count_python_occurrences(file_name)

# Task b: Search for lines that contain the word "python"

print("Lines containing the word 'python':")

search_lines_with_python(file_name)

# Task c: Replace a specific word or phrase with another word or phrase in a text file

replace_word_in_file(file_name, 'python', 'java')

# Display the modified file

print("Modified file contents:")

with open(file_name, 'r') as file:

for line in file:

print(line.rstrip())

print("\n\nShashank Pratap 22MCA1061")

Screenshot:
7. Write a python program to validate a password using regular expression. Conditions for a valid
password are as follows:

a. It should have at least one number.

b. Should have at least one uppercase and one lowercase character.

c. Should have at least one special symbol.

d. Should be between 6 to 20 characters long.

Solution:

import re

def validate_password(password):

pattern = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@#$%^&+=!])(?=.*[a-zA-Z0-
9@#$%^&+=!]).{6,20}$"

if re.match(pattern, password):

print("Password is valid")

else:

print("Password is not valid")

# Testing the function


input_password = input("Enter a password: ")

validate_password(input_password)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:

8. Extract the protocol and the hostname from the given URL ‘https://www.deeplearning.org/

Solution:

from urllib.parse import urlparse

def extract_protocol_and_hostname(url):

parsed_url = urlparse(url)

protocol = parsed_url.scheme

hostname = parsed_url.hostname

if protocol and hostname:

print("Protocol:", protocol)
print("Hostname:", hostname)

else:

print("Invalid URL")

# Testing the function

input_url = 'https://www.deeplearning.org/'

extract_protocol

print("\n\nShashank Pratap 22MCA1061")

Screenshot:

9. Extract the protocol and the hostname from the given URL ‘https://www.deeplearning.org/

Solution:

from urllib.parse import urlparse


def extract_protocol_hostname_port(url):

parsed_url = urlparse(url)

protocol = parsed_url.scheme

hostname = parsed_url.hostname

port = parsed_url.port

if protocol and hostname and port:

print("Protocol:", protocol)

print("Hostname:", hostname)

print("Port:", port)

else:

print("Invalid URL")

# Testing the function

input_url = 'file://localhost:4040/abe_file'

extract_protocol_hostname_port(input_url)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:
10. How to validate the following Indian mobile numbers using regular expression?

The following formats:

1. 8880344456
2. +918880344456
3. +91 8880344456
4. +91-8880344456
5. 08880344456
6. 918880344456 to be verified
Solution:

import re

def validate_mobile_number(number):

pattern = r'^(\+?91[\-\s]?)?[6-9]\d{9}$'

if re.match(pattern, number):

print("Valid mobile number")


else:

print("Invalid mobile number")

# Testing the function

numbers = [

"8880344456",

"+918880344456",

"+91 8880344456",

"+91-8880344456",

"08880344456",

"918880344456"

for number in numbers:

validate_mobile_number(number)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:
11. Verify the validity the email addresses using regular expression

Input: ankitrai326@gmail.com Output: Valid Email

Input: my.ownsite@ourearth.org Output: Valid Email

Input: ankitrai326.com Output: Invalid Email

Input: Anirudh @ com Output: Invalid Email

Input: AC .com Output: Invalid Email

Input: 123 @.com Output: Invalid Email

Solution:

import re

def validate_email(email):

pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

if re.match(pattern, email):
print("Valid Email")

else:

print("Invalid Email")

# Testing the function

emails = [

"ankitrai326@gmail.com",

"my.ownsite@ourearth.org",

"ankitrai326.com",

"Anirudh @ com",

"AC .com",

"123 @.com"

for email in emails:

validate_email(email)

print("\n\nShashank Pratap 22MCA1061")

Screenshot:

You might also like