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.

Similar Reads

Regex Module in Python

Python has a built-in module named “re” that is used for regular expressions in Python. We can import this module by using the import statement....

How to Use RegEx in Python?

...

Metacharacters

You can use RegEx in Python after importing re module....

Special Sequences

...

RegEx Functions

Metacharacters are the characters with special meaning....

1. re.findall()

...

2. re.compile()

...

3. re.split()

...

4. re.sub()

...

5. re.subn()

...

6. re.escape()

Special sequences do not match for the actual character in the string instead it tells the specific location in the search string where the match must occur. It makes it easier to write commonly used patterns....

7. re.search()

re module contains many functions that help us to search a string for a match....

SETS

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found....

Match Object

...