the code given below:
import java.util.regex.*; public class regex { public static void main(string[] args) { pattern p = pattern.compile("\\d*"); matcher m = p.matcher("ab56ef"); system.out.println("pattern " + m.pattern()); while (m.find()) { system.out.print("index: " + m.start() + " " + m.group()); } } }
the result is:
index: 0 index: 1 index: 2 56 index: 4 index: 5 index: 6
since "ab34ef" length 6, string's highest index 5.
why there match @ index 6? thank in advance!
you have 6 indices returned because there 6 matches here since \d*
can match empty string. there empty string before each character in input string, because regex engine processing text @ each position looking boundaries or specific characters.
here visualization:
here, engine examines beginning of string, , says: "i see no digit, can return match, since number of digits can 0". returns empty string match, , goes on b
. , on until end of string.
if need find numbers, use +
quantifier \d
shorthand class.
see ideone demo
Comments
Post a Comment