Using alternation and grouping constructs

后端 未结 2 447
被撕碎了的回忆
被撕碎了的回忆 2021-01-24 07:52

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

相关标签:
2条回答
  • 2021-01-24 08:15

    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])$
    
    0 讨论(0)
  • 2021-01-24 08:22

    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]
    
    0 讨论(0)
提交回复
热议问题