telebog
telebog

Reputation: 1896

Same named group multiple times in JVM?

Is there a way in JVM (preferably in Java or a small library) to have the following regex (?<Hour>\\d\\d)* and to be able to extract all the hours? For example if "12131415" is given then to be able to get a collections of hours something like {12,13,14,15}.

Does groovy support this?

Upvotes: 1

Views: 54

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174826

The below regex would capture each two digits and stores it into separate groups.

Your code would be,

String s = "12131415";
Pattern p = Pattern.compile("(?<Hour>\\d{2})");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}

IDEONE

Upvotes: 1

Related Questions