How to extract matches from (\d+[hms])+ ?

Wang Weijun at
Thu Sep 25 04:51:59 UTC 2014

Hi Sherman

I want to match a time duration like "1h20m30s" and "2h". It looks like if I directly use the pattern "((\\d+)([hms]))+", group(2) and group (3) only return the last match (i.e. 30 and s for 1h20m30s). So I tried multiple matching with "(\\d)([hms])" only, but find() does not always match from the beginning, and lookingAt() does not advance after one call.

This is my code now;

int start = 0;
while (true) {
  if (!m.find() || m.start() != start) {
    throw new Exception();
  start = m.end();
  if (m.hitEnd()) break;

Is this the correct way?


More information about the core-libs-dev mailing list