How to extract matches from (\d+[hms])+ ?
Guy Steele
guy.steele at oracle.com
Thu Sep 25 05:06:35 UTC 2014
(A lurker sticking his nose in here! :-) Is it your intent also to match "30s1h" or "20m30m" as a time duration?
If not, you might be better off with a pattern such as "((\\d+)h)?((\\d+)m)?((\\d+)s)?" and then the whole problem caused by the outer "+" iteration disappear (but you may need to check whether the original string was empty).
But maybe that takes all the fun out of it.
--Guy Steele
On Sep 25, 2014, at 12:51 AM, Wang Weijun <weijun.wang at oracle.com> wrote:
> 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