A literal regular expression or replacement is one that re refuses
What it finds
re.compile("(\d+"), re.sub(r"(\w+)", r"\2", text): a pattern written as a literal that re refuses raises re.error every time the line runs, and so does a replacement that refers to a group the pattern does not have.
Checked are literal patterns given to the functions of re, for unbalanced parentheses, unclosed classes, repeats of nothing, backward ranges, unknown escapes and group syntax, duplicate or undefined group names and references to missing groups, and literal replacements of re.sub and re.subn. Patterns in verbose mode, and calls whose flags are not plain re constants, are left alone.
From the rule's source, invalid_regex.rs.
import re def parse(text, flags): a = re.compile(r"(\d+")tx0 reports: re refuses this pattern: a ( is never closed b = re.search(r"[z-a]", text)tx0 reports: re refuses this pattern: the range z-a runs backwards c = re.match(r"*start", text)tx0 reports: * has nothing to repeat d = re.sub(r"(\w+)", r"\2", text)tx0 reports: re refuses this replacement: \2 refers to group 2 e = re.sub(r"(?P<word>\w+)", r"\g<other>", text)tx0 reports: \g<other> refers to a group that the pattern does not have f = re.compile(r"\q")tx0 reports: \q is not an escape that re knows g = re.compile(r"(?P<n>a)(?P<n>b)")tx0 reports: the group name n is defined twice h = re.compile(r"(?x) ( a b") i = re.compile(r"(a", flags) j = re.sub(r"(\w+) (\w+)", r"\2 \1", text) k = re.compile(r"[\w.-]+@[\w-]+\.\w{2,}", re.IGNORECASE) return a, b, c, d, e, f, g, h, i, j, k Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/python:invalid-regex" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:invalid-regex] reason="..."tx0 explain tx0/python:invalid-regex shows the same in the terminal. Found a false positive? Report it.