Professional Documents
Culture Documents
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):
else:
check_string_characters(input_string)
Screenshot:
2. Write a Python program that matches a string that has an ‘a’ followed by
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):
if re.match(pattern_b, string):
if re.match(pattern_c, string):
match_patterns(input_string)
Screenshot:
3. Write a Python program that matches a word containing 'z' in the string “The quick
#3
import re
def match_word_with_z(string):
pattern = r'\b\w*z\w*\b'
if matches:
else:
input_string = "The quick brown fox jumps over the lazy dog"
match_word_with_z(input_string)
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'
if matches:
else:
input_string = "The quick brown fox jumps over the lazy dog"
match_word_with_z(input_string)
Screenshot:
5. Write a Python program to find the substring “exercises” within a string “Python exercises, PHP
exercises, C# exercises”
Solution:
index = string.find(substring)
if index != -1:
else:
substring = "exercises"
find_substring(input_string, substring)
6. Create your own text file. The text file should have multiple lines of text that should
a. Use regular expressions to count the number of times the word “python”
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
Solution:
import re
# Task a: Count the number of times the word "python" appears in the file
def count_python_occurrences(file_name):
pattern = r'python'
count = len(matches)
def search_lines_with_python(file_name):
pattern = r'^.*python.*$'
print(line.rstrip())
# Task c: Replace a specific word or phrase with another word or phrase in a text file
contents = file.read()
file.write(modified_contents)
file_name = 'sample_file.txt'
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)
search_lines_with_python(file_name)
# Task c: Replace a specific word or phrase with another word or phrase in a text file
print(line.rstrip())
Screenshot:
7. Write a python program to validate a password using regular expression. Conditions for a valid
password are as follows:
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:
validate_password(input_password)
Screenshot:
8. Extract the protocol and the hostname from the given URL ‘https://www.deeplearning.org/
Solution:
def extract_protocol_and_hostname(url):
parsed_url = urlparse(url)
protocol = parsed_url.scheme
hostname = parsed_url.hostname
print("Protocol:", protocol)
print("Hostname:", hostname)
else:
print("Invalid URL")
input_url = 'https://www.deeplearning.org/'
extract_protocol
Screenshot:
9. Extract the protocol and the hostname from the given URL ‘https://www.deeplearning.org/
Solution:
parsed_url = urlparse(url)
protocol = parsed_url.scheme
hostname = parsed_url.hostname
port = parsed_url.port
print("Protocol:", protocol)
print("Hostname:", hostname)
print("Port:", port)
else:
print("Invalid URL")
input_url = 'file://localhost:4040/abe_file'
extract_protocol_hostname_port(input_url)
Screenshot:
10. How to validate the following Indian mobile numbers using regular expression?
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):
numbers = [
"8880344456",
"+918880344456",
"+91 8880344456",
"+91-8880344456",
"08880344456",
"918880344456"
validate_mobile_number(number)
Screenshot:
11. Verify the validity the email addresses using regular expression
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")
emails = [
"ankitrai326@gmail.com",
"my.ownsite@ourearth.org",
"ankitrai326.com",
"Anirudh @ com",
"AC .com",
"123 @.com"
validate_email(email)
Screenshot: