Python re Module – Key Functions Explained
1. groups()
Returns a tuple containing the text matched by each capturing group in the regex.
Example: [Link](r"(P?\w+)-(P?\w+)", "Python-Ryzen").groups() → ('Python', 'Ryzen')
2. group()
Returns the exact text matched by a regex search or match object.
Example: [Link](r"(P?\w+)-(P?\w+)", "Python-Ryzen").group() → "Python-Ryzen"
3. [Link]()
Searches the entire string for the first match of the pattern.
Example: [Link]("fun", "Python is fun") → Match found
4. [Link]()
Checks for a match only at the beginning of the string.
Example: [Link]("Python", "Python is fun") → Match found
Example: [Link]("fun", "Python is fun") → No match
5. [Link]()
Matches the entire string exactly with the pattern.
Example: [Link]("Python is fun", "Python is fun") → Match found
6. [Link]()
Returns a list of all matching substrings.
Example: [Link]("\d+", "A1 B22 C333") → ['1', '22', '333']
7. [Link]()
Returns an iterator of match objects.
Useful for retrieving match positions using start() and end().
8. [Link]()
Splits a string using a regex pattern.
Example: [Link](",\s*", "a, b, c") → ['a', 'b', 'c']
9. [Link]()
Replaces matched patterns with a new string.
Example: [Link]("\d", "#", "A1B2") → "A#B#"
Summary Table
search → anywhere in string
match → start only
fullmatch → entire string
findall → list of matches
finditer → iterator of matches
split → split string
sub → replace matches