Regular Expressions (re)
A Regular Expression (RegEx) is a sequence of characters that forms a search pattern. Python has a built-in package called re to work with RegEx.
1. Basic Search
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
if x:
print("YES! We have a match!")
2. Core Functions
- findall(): Returns a list containing all matches.
- search(): Returns a Match object if there is a match anywhere in the string.
- split(): Returns a list where the string has been split at each match.
- sub(): Replaces one or many matches with a string.
3. Practical Validation
Commonly used for validating email addresses or identifying patterns in large text files.
emails = "Contact me at info@example.com or support@site.org"
matches = re.findall(r'[\w\.-]+@[\w\.-]+', emails)
print(matches) # ['info@example.com', 'support@site.org']
RegEx Tip: Use Raw Strings (prefixing with r'') when writing patterns to avoid issues with backslashes.