Pattern p = Pattern.compile("\\d?");
Matcher m = p.matcher("ab34ef");
boolean b = false;
while (m.find())
{
System.out.print(m.start());// + m.group());
}
But string total length is 6. So How m.start will give 6 in the output, as index starts
from 0.
\d? matches zero or one character, so it starts beyond the last character of the string as well, as a zero-width match.
Note that your output is not in fact attained by \d?, but by \d*. You should change either one or the other to make the question self-consistent.
\d? matches zero or one digit, which matches every digit, but also matches every character boundary.
Try matching at least one digit:
Pattern p = Pattern.compile("\\d+");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With