A ^ or $ in a regular expression applies only to the first or last alternative, not to all of them
What it finds
^yes|no$ reads as (^yes)|(no$): | binds more loosely than anything else, so the ^ belongs to the first alternative only and the $ to the last. A string that contains no anywhere before its end, or starts with yes, matches. The anchors were meant for the whole choice, as in ^(yes|no)$.
Reported is a pattern whose top level is a choice of alternatives, when the first one starts with ^ or the last one ends with $ but not all of them do.
From the rule's source, regex_anchor_binds_one_alternative.rs.
import java.util.regex.Pattern; class Answers { Pattern a = Pattern.compile("^yes|no");tx0 reports: ^ applies to one alternative only Pattern b = Pattern.compile("yes|no$");tx0 reports: $ applies to one alternative only Pattern c = Pattern.compile("^(yes|no)$"); Pattern d = Pattern.compile("^yes$|^no$");} 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/java: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/java:regex-anchor-binds-one-alternative] reason="..."tx0 explain tx0/java:regex-anchor-binds-one-alternative shows the same in the terminal. Found a false positive? Report it.