How regular expression OR operator is evaluated

后端 未结 2 546
忘了有多久
忘了有多久 2021-01-18 08:43

In T-SQL I have generated UNIQUEIDENTIFIER using NEWID() function. For example:

723952A7-96C6-421F-961F-80E66A4F29D2

Then,

2条回答
  •  走了就别回头了
    2021-01-18 09:18

    If you are interested in what happens when you use | alternation operator, the answer is easy: the regex engine processes the expression and the input string from left to right.

    Taking the pattern you have as an example, ^.{8}|.{12}$|.{4} starts inspecting the input string from the left, and checks for ^.{8} - first 8 characters. Finds them and it is a match. Then, goes on and finds the last 12 characters with .{12}$, and again there is a match. Then, any 4-character strings are matched.

    Regular expression visualization

    Debuggex Demo

    Next, you have ^.{8}|.{4}|.{12}$. The expression is again parsed from left to right, first 8 characters are matched first, but next, only 4-character sequences will be matched, .{12} won't ever fire because there will be .{4} matches!

    Regular expression visualization

    Debuggex Demo

提交回复
热议问题