re#

The re module in Python realizes regular expressions. See the full official description in the corresponding official HOWTO.

import re

Usage#

The re module provides several common operations: match, search, findall, and finditer. You can use these functions directly from the re module, which is the easiest option for experiments and command-line operations. Alternatively, you can use re.compile to precompile regular expressions—this approach is widely used in production, as it allows for efficient reuse when processing multiple strings.


The following code shows how re.findall can be used.

re.findall(r'\b\w{5}\b', 'Hello, world!')
['Hello', 'world']

The same example that uses re.compile to build the expression and use findall from it.

expression_object = re.compile(r'\b\w{5}\b')
expression_object.findall('Hello, world!')
['Hello', 'world']

Find all#

The findall method allows you to extract all patterns as a list of elements.


The following cell extracts all words that are surrounded by [].

re.findall(
    r'\[(.*?)\]',
    '[wow] -> [this] -> [is] -> [cool]'
)
['wow', 'this', 'is', 'cool']