A pattern starts with ^ or ends with $ around several alternatives, but the anchor applies only to the first or last of them
What it finds
| binds more loosely than anything else, so ^cat|dog$ means "cat at the start, or dog at the end", not "exactly cat or dog". Grouping the alternatives, as in ^(?:cat|dog)$, gives the anchors to all of them.
From the rule's source, regex_anchor_binds_one_alternative.rs.
import re a = re.match(r"^cat|dog$", s)tx0 reports: the anchors apply only to the first and the last alternativeb = re.match(r"^(?:cat|dog)$", s)c = re.match(r"^cat$|^dog$", s) Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:regex-anchor-binds-one-alternative" = "warning"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:regex-anchor-binds-one-alternative] reason="..."tx0 explain tx0/python:regex-anchor-binds-one-alternative shows the same in the terminal. Found a false positive? Report it.