c# - How regular expression OR operator is evaluated -


in t-sql have generated uniqueidentifier using newid() function. example:

723952a7-96c6-421f-961f-80e66a4f29d2 

then, dashes (-) removed , looks this:

723952a796c6421f961f80e66a4f29d2 

now, need turn string above valid uniqueidentifier using following format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx , setting dashes again.

to achieve this, using sql clr implementation of c# regexmatches function ^.{8}|.{12}$|.{4} regular expression gives me this:

select * [dbo].[regexmatches] ('723952a796c6421f961f80e66a4f29d2', '^.{8}|.{12}$|.{4}') 

enter image description here

using above, can build again correct uniqueidentifier wondering how or operator evaluated in regular expression. example, following not work:

select * [dbo].[regexmatches] ('723952a796c6421f961f80e66a4f29d2', '^.{8}|.{4}|.{12}$') 

enter image description here

is sure first regular expression first match start , end of string, other values , returning matches in order (i have issues if example, 96c6 matched after 421f).

if interested in happens when use | alternation operator, answer easy: regex engine processes expression left right.

taking pattern have example, ^.{8}|.{12}$|.{4} starts inspecting input string left, , checks ^.{8} - first 8 characters. finds them , match. then, goes on , finds last 12 characters .{12}$, , again there match. then, 4-character strings matched.

regular expression visualization

debuggex demo

next, have ^.{8}|.{4}|.{12}$. expression again parsed left right, first 8 characters matched first, but next, 4-character sequences matched, .{12} won't ever fire because there .{4} matches!

regular expression visualization

debuggex demo


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -