0% found this document useful (0 votes)
2 views1 page

Python Regex Functions

The document explains key functions of the Python re module, including groups(), group(), re.search(), re.match(), and others. It provides examples for each function, illustrating their usage in regex operations such as searching, matching, finding, splitting, and substituting strings. A summary table is included to highlight the main functionalities of each method.

Uploaded by

sibuswayam1029
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Python Regex Functions

The document explains key functions of the Python re module, including groups(), group(), re.search(), re.match(), and others. It provides examples for each function, illustrating their usage in regex operations such as searching, matching, finding, splitting, and substituting strings. A summary table is included to highlight the main functionalities of each method.

Uploaded by

sibuswayam1029
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like