RegEx Functions
re module contains many functions that help us to search a string for a match.
Let’s see various functions provided by this module to work with regex in Python.
Function | Description |
---|---|
re.findall() | finds and returns all matching occurrences in a list |
re.compile() | Regular expressions are compiled into pattern objects |
re.split() | Split string by the occurrences of a character or a pattern. |
re.sub() | Replaces all occurrences of a character or patter with a replacement string. |
re.escape() | Escapes special character |
re.search() | Searches for first occurrence of character or pattern |
Let’s see the working of these RegEx functions with definition and examples:
Regular Expression (RegEx) in Python with Examples
A Regular Expression or RegEx is a special sequence of characters that uses a search pattern to find a string or set of strings.
It can detect the presence or absence of a text by matching it with a particular pattern and also can split a pattern into one or more sub-patterns.