Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Q1. python program to check if a string starts with the and end with spain use regex
- import re
- def check_string(input_string):
- pattern = r'^the.*spain$'
- if re.match(pattern, input_string, re.IGNORECASE):
- return True
- else:
- return False
- test_string1 = input("Enter string: ")
- print(check_string(test_string1))
- #output:
- Enter string: the spain
- True
- Enter string: the spain not
- False
- #Q. program to find all lowercase charcters aplhabetically between a and m, use regex
- import re
- def find_lower_chars(input_string):
- pattern = r'[a-m]'
- result = re.findall(pattern, input_string)
- return result
- user_input = input("Enter a string: ")
- lowercase_chars = find_lower_chars(user_input)
- print("Lowercase characters between 'a' and 'm':", lowercase_chars)
- #output:
- Enter a string: The quick brown fox jumps over the lazy dog
- Lowercase characters between 'a' and 'm': ['h', 'e', 'i', 'c', 'k', 'b', 'f', 'j', 'm', 'e', 'h', 'e', 'l', 'a', 'd', 'g']
- #Q. program to search for the first white space character in the string
- import re
- txt = "The rain in Spain"
- x = re.search("\s", txt)
- print("The first white-space character is located in position:", x.start())
- #Output:
- The first white-space character is located in position: 3
- #Q. program to split at each whitespace character using regex
- import re
- def split_at_whitespace(input_string):
- return re.split(r'\s+', input_string)
- user_input = input("Enter a string: ")
- result = split_at_whitespace(user_input)
- print("Split at each whitespace character:", result)
- #Output:
- Enter a string: This is a demo input.
- Split at each whitespace character: ['This', 'is', 'a', 'demo', 'input.']
- #Q. replace every white space character with the number nine
- import re
- def replace_whitespace_with_nine(input_string):
- return re.sub(r'\s', '9', input_string)
- user_input = input("Enter a string: ")
- result = replace_whitespace_with_nine(user_input)
- print("String with every whitespace character replaced with '9':", result)
- #Output:
- Enter a string: This is a demo input
- String with every whitespace character replaced with '9': This9is9a9demo9inputp
- #Q: check if email valid or not:
- import re
- def validate_email(email):
- pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
- regex = re.compile(pattern)
- return bool(regex.match(email))
- user_email = input("Enter your email address: ")
- if validate_email(user_email):
- print("Valid email address!")
- else:
- print("Invalid email address. Please enter a valid email.")
- #Output:
- Enter your email address: arijit@gmail.com
- Valid email address!
- Enter your email address: arijit@gmail
- Invalid email address. Please enter a valid email.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement