I have a string
String text="abc19xyz87nag";
I need to get only the numbers out of it, so I applied "\\D+"
regex as below,
String text="abc19xyz87nag";
String[] tks=text.split("\\D+");
But I see a empty token in the beginning of the array
How ever I have found out two other solutions anyway as below
Using scanner
Scanner sc = new Scanner(text).useDelimiter("\\D+");
while(sc.hasNext()) {
System.out.println(sc.nextInt());
}
Using Pattern and Matcher
Matcher m = Pattern.compile("\\d+").matcher(text);
while (m.find()) {
System.out.println(m.group());
}
So Why string split is leaving empty token at the beginning?
Do I need to change the regex to avoid it?
Any help is appreciated