Python: Regex Functions

By Xah Lee. Date: . Last updated: .

Here's a summary of regex functions.

re.search(regex, text)
Return match object if found, else None
re.search
re.match(regex, text)
Similar to re.search(), but match starts at beginning of string.
re.match
re.split(regex, text)
Return a list.
re.split
re.findall(regex, text)
Return a list of non-overlapping (repeated) matches.
re.findall
re.finditer()
Similar to re.findall(), but returns a iterator.
re.finditer
re.sub(regex, repl, text)
Does replacement. Returns the new string.
re.sub
re.subn()
Similar to re.sub(), but returns a tuple. 1st element is the new string, 2nd is number of replacement.
re.subn
re.escape(str)
Add backslash to string for feeding it to regex as pattern. Return the new string.
re.escape

Exception Error

Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern.

Python, Regular Expression