7 chars · 1 line
8 chars · 1 line
7 chars · 1 line
8 chars · 1 line
Regex grouping behavior
The valid pattern ^cat|dog$ is parsed as one alternative that starts with cat or another that ends with dog. That is why catfish matches. Group the alternatives when the same anchors should apply to both. This tool is free to use. No account or payment is required.
Last updated
Without grouping, ^cat|dog$ has two branches: ^cat and dog$. Parentheses make the intended common boundaries explicit and easier to review.
Group cat and dog before applying both anchors.
^cat|dog$^(cat|dog)$Common questions
Because the first alternation branch is ^cat, which only requires cat at the start.
Yes. ^(?:cat|dog)$ groups the alternatives without creating a capture.
Yes. Grouping is the clearest way to show exactly which parts belong to each alternative.
Debugging a specific Regex issue? Browse Regex troubleshooting.