import re
# 1. Match Lowercase Letters
# This will match any string containing only lowercase letters
def match_lowercase(text):
pattern = r'^[a-z]+$' # ^ indicates start of string, $ indicates end, [a-z]+ matches one or more lowercase letters
if re.match(pattern, text):
print(f"'{text}' contains only lowercase letters.")
else:
print(f"'{text}' does not contain only lowercase letters.")
# 2. Match Uppercase Letters
# This will match any string containing only uppercase letters
def match_uppercase(text):
pattern = r'^[A-Z]+$' # [A-Z]+ matches one or more uppercase letters
if re.match(pattern, text):
print(f"'{text}' contains only uppercase letters.")
else:
print(f"'{text}' does not contain only uppercase letters.")
# 3. Match Numbers (Integers)
# This will match a string consisting only of digits
def match_numbers(text):
pattern = r'^\d+$' # ^\d+$ matches one or more digits (0-9)
if re.match(pattern, text):
print(f"'{text}' is a number.")
else:
print(f"'{text}' is not a number.")
# 4. Match Odd Numbers (Example: 1, 3, 5, 7, 9, etc.)
# This will check if the number is odd by matching digits ending with 1, 3, 5, 7, or 9
def match_odd_number(text):
pattern = r'^[13579]$' # ^[13579]$ matches a single odd digit
if re.match(pattern, text):
print(f"'{text}' is an odd number.")
else:
print(f"'{text}' is not an odd number.")
# 5. Match Even Numbers (Example: 0, 2, 4, 6, 8, etc.)
# This checks if the number is even by matching digits ending with 0, 2, 4, 6, or 8
def match_even_number(text):
pattern = r'^[02468]$' # ^[02468]$ matches a single even digit
if re.match(pattern, text):
print(f"'{text}' is an even number.")
else:
print(f"'{text}' is not an even number.")
# 6. Validate Email Address
# This will validate a basic email address using a regex pattern
def validate_email(text):
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' # Basic email format pattern
if re.match(pattern, text):
print(f"'{text}' is a valid email.")
else:
print(f"'{text}' is not a valid email.")
# 7. Validate Phone Number (US Format: (XXX) XXX-XXXX)
# This checks if the phone number is in the format (123) 456-7890
def validate_phone_number(text):
pattern = r'^\(\d{3}\) \d{3}-\d{4}$' # (\d{3}) matches 3 digits in parentheses, followed by space, 3 digits, hyphen, and 4 digits
if re.match(pattern, text):
print(f"'{text}' is a valid phone number.")
else:
print(f"'{text}' is not a valid phone number.")
# 8. Find All Occurrences of a Word in a Text
# This will find all occurrences of the word 'Python' (case-insensitive)
def find_word_occurrences(text):
pattern = r'python' # Matching the word 'python'
results = re.findall(pattern, text, re.IGNORECASE) # re.IGNORECASE allows case-insensitive matching
print(f"Found {len(results)} occurrences of 'python'.")
# 9. Extract Dates from Text (Format: DD-MM-YYYY)
# This will match and extract all dates in the form of dd-mm-yyyy
def extract_dates(text):
pattern = r'\b\d{2}-\d{2}-\d{4}\b' # \b ensures we match a complete date (not part of a longer number)
dates = re.findall(pattern, text)
print("Extracted dates:", dates)
# 10. Replace All Spaces with Hyphens
# This will replace all spaces in the input text with hyphens
def replace_spaces_with_hyphens(text):
pattern = r'\s+' # \s+ matches one or more whitespace characters
modified_text = re.sub(pattern, '-', text) # re.sub replaces the matched pattern with '-'
print(f"Modified text: {modified_text}")
# 11. Check for a Hexadecimal Color Code
# This will match a hexadecimal color code like #FF5733
def validate_hex_color(text):
pattern = r'^#[0-9A-Fa-f]{6}$' # # followed by exactly 6 hexadecimal characters
if re.match(pattern, text):
print(f"'{text}' is a valid hex color code.")
else:
print(f"'{text}' is not a valid hex color code.")
# Example Usage
if __name__ == "__main__":
match_lowercase("hello") # Expected: contains only lowercase
match_uppercase("HELLO") # Expected: contains only uppercase
match_numbers("123456") # Expected: is a number
match_odd_number("3") # Expected: is an odd number
match_even_number("4") # Expected: is an even number
validate_email("example@gmail.com") # Expected: valid email
validate_phone_number("(123) 456-7890") # Expected: valid phone number
find_word_occurrences("Python is great. I love python.") # Expected: 2 occurrences
extract_dates("The event is on 25-12-2023, and the next is on 01-01-2024.") # Expected: 2 dates
replace_spaces_with_hyphens("This is a test.") # Expected: This-is-a-test.
validate_hex_color("#FF5733") # Expected: valid hex color code
đ Benchmarking Databases for Real-Time Analytics Applications
Benchmarking Timescale, Clickhouse, Postgres, MySQL, MongoDB, and DuckDB for real-time analytics. Introducing RTABench đ
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)