Creating a regular expression in Python

You can construct a regular expression in Python using the re module that is natively shipped with the Python installation. There are several methods inside this module, such as search(), sub(), split(), compile(), and findall(), which will return the result as a regex object. Here is a summary of the use of each function:

Function Name

Usage

search()

Search and match the first occurrence of the pattern.

findall()

Search and match all occurrences of the pattern and return the result as a list.

Finditer()

Search and match all occurrences of the pattern and return the result as an iterator.

compile()

Compile the regex into a pattern object that has methods for various operations, ...

Get Hands-On Enterprise Automation with Python. now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.