regex - regular expressions result multiplicity -


given regular expression "\d", match each digit in "a123b456" (i.e. 1, 2, 3, 4, 5, 6).

given regular expression "\d\d" , same teststring, seems match "12" , "45" - @ least http://regexpal.com/ says, regex evaluator created myself using c++ textbook (which uses boost/regex).

why doesn't second 1 match "23" , "56", well, or, if behaviour correct one, why first 1 match each number?

why doesn't second 1 match "23" , "56"?

because that's overlapping match expecting regex give you. once part of string matched pattern, won't matched again same pattern. so, since 2 been included in previous match 12, gone. regex move onto next character 3. , following character, cannot see 3 included part of string matching \d\d. next substring matching pattern found @ 45.

try changing string to:

"a1234b456" 

and 3 matches - 12, 34, , 45.

however, can overlapping matches using positive look-ahead - (?=\d\d), because look-arounds 0-length match expression. won't consume characters match.

and fyi, can use \d{2} instead of \d\d.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -