Skip to content

unnecessary-regular-expression-compile

Preview (since 0.17.2) · Related issues · View source

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for re.compile() calls whose result is used immediately and only once, e.g. re.compile(pattern).match(string).

Why is this bad?

Compiling a pattern inline and immediately calling a method on it is equivalent to calling the corresponding top-level re function, such as re.match or re.sub. The top-level function is shorter.

Example

import re

re.compile(pattern).match(string)

Use instead:

import re

re.match(pattern, string)

References