java - Why empty regex and empty capturing group regex return string length plus one results -
how explain empty regex , empty capturing group regex return string length plus 1 results?
code
public static void main(string... args) { { system.out.format("pattern - empty string\n"); string input = "abc"; pattern pattern = pattern.compile(""); matcher matcher = pattern.matcher(input); while (matcher.find()) { string s = matcher.group(); system.out.format("[%s]: %d / %d\n", s, matcher.start(), matcher.end()); } } { system.out.format("pattern - empty capturing group\n"); string input = "abc"; pattern pattern = pattern.compile("()"); matcher matcher = pattern.matcher(input); while (matcher.find()) { string s = matcher.group(); system.out.format("[%s]: %d / %d\n", s, matcher.start(), matcher.end()); } } } output
pattern - empty string []: 0 / 0 []: 1 / 1 []: 2 / 2 []: 3 / 3 pattern - empty capturing group []: 0 / 0 []: 1 / 1 []: 2 / 2 []: 3 / 3
regex engines consider positions before , after characters, too. can see fact have things ^ (start of string), $ (end of string) , \b word boundary, match @ positions without matching characters (and therefore between/before/after characters). therefore have n-1 positions between characters have considered, first , last position (because ^ , $ match there respectively), gives n+1 candidate positions. of match unrestrictive empty pattern.
so here matches:
" b c " ^ ^ ^ ^ which n+1 n characters.
you same behavior other patterns allow zero-length matches , don't find longer ones in pattern. instance, try \d*. cannot find digits in input string, * gladly return zero-length matches.
Comments
Post a Comment