c# - Regex match one digit or two -


if

(°[0-5]) 

matches °4

and

((°[0-5][0-9])) 

matches °44

why this

((°[0-5])|(°[0-5][0-9])) 

match °4 not °44?

because when use logical or in regex regex engine returns first match when find match first part of regex (here °[0-5]), , in case since °[0-5] match °4 in °44 returns °4 , doesn't continue match other case (here °[0-5][0-9]):

((°[0-5])|(°[0-5][0-9])) 

a|b, , b can arbitrary res, creates regular expression match either or b. arbitrary number of res can separated '|' in way. can used inside groups (see below) well. as target string scanned, res separated '|' tried left right. when 1 pattern matches, branch accepted. means once matches, b not tested further, if produce longer overall match. in other words, '|' operator never greedy. match literal '|', use \|, or enclose inside character class, in [|].


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -