Code-Memo

Regular Expressions

Regular expressions (regex) are sequences of characters that form search patterns, primarily used for string matching.

Basic Example

import re

pattern = r'\d+'  # Matches one or more digits
string = "The house number is 123 and the zip code is 45678."
matches = re.findall(pattern, string)
print(matches)  # Output: ['123', '45678']

Advanced Example: Email Validation

pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
email = "example@example.com"
match = re.match(pattern, email)
if match:
    print("Valid email")
else:
    print("Invalid email")

Using re Functions