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

Wang Weijun weijun.wang at oracle.com
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();
  print(m.group(1), m.group(2));
  if (m.hitEnd()) break;
}
print("Done");

Is this the correct way?

Thanks
Max




More information about the core-libs-dev mailing list