What is the scope of the alternation operator |? In some cases, it appears that everything to the left of | is one alternative and everything to the right of the | is the second alternative. Why are the alternatives not limited to the capture groups or tokens immediately preceding and following the |?
I am testing with RegExr, but I will be running the regular expressions in Java.
I have two short regular expressions:
(\d{3})\W+(SSN)
(Matches in bold.)
SSNx 123 SSN
(SSN)\W+(\d{3})
(Matches in bold.)
SSN 123 SSN
If I combine the regular expressions like this:
(\d{3})\W+(SSN)|(SSN)\W+(\d{3})
The regular expression matches (in bold)
SSN 123 SSN
SSNx 123 SSN
Why was it unnecessary for me to add parentheses like this?
((\d{3})\W+(SSN))|((SSN)\W+(\d{3}))
I expected the alternation operation to alternate only between the surrounding capture groups, e.g. "SSN" OR "SSN". If that were the case, then the results would look like this:
((\d{3})\W+(SSN)\W+(\d{3}))
No matches.