Originally I wanted a regex parsing month numbers. At first I came up with the following regex:
^([1-9]{1})|(1[012])$
and it matched any positi
Read the first regular expression like this:
^([1-9]{1}) # match this
| # ...OR...
(1[012])$ # match this
Either match a digit 1-9 at the beginning of the string and store that in group #1, or match 10-12 at the end of the string and store that in group #2.
The first successful match is used, so when you match against 10
the ^([1-9]{1})
part of the regex matches. You can see why 20
is a match with this broken regex.
Also, you print out only group #1's contents and ignore group #2's. So if the second set of parentheses had happened to match, you wouldn't see that in your printout.
if (m.Success)
Console.WriteLine(m.Groups[0].Value);
Your second regex fixes the problem by surrounding the two |
alternatives with parentheses, leaving the ^
and $
anchors on the outside, and leaving only one set of parentheses so the result is always in group #1.
And for what it's worth, the {1}
is unnecessary. You could write:
^([1-9]|1[012])$
The alternation operator has the lowest precedence of all regex operators.
The difference between the two regular expressions, interpreted literally, is this:
( [BEGIN]([1-9]) ) OR ( (1[012])[END] )
vs
[BEGIN] ( [1-9] OR 1[012] ) [END]