Given this String: "a001212a a1212a a00333a a00334a abvbv";
I'm trying to pick the third number just by changing the regex. But I can't figure it out. I am only allowed to change the regex. My code below works if i change the argument to matcher.group(2) but not for group(1).
Help?
String nthNumber(String string) {
Pattern pat = Pattern.compile(
"(\\D+0*(\\d+)\\D+){3,}.*"
);
Matcher matcher = pat.matcher(string);
matcher.matches();
return matcher.group(1);
}
You can make the first group a non-capturing group like below. Then, group(1) should work.
String nthNumber(String string) {
Pattern pat = Pattern.compile(
"(?:\\D+0*(\\d+)\\D+){3,}.*"
);
Matcher matcher = pat.matcher(string);
matcher.matches();
return matcher.group(1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.