How to extract matches from (\d+[hms])+ ?
Xueming Shen
xueming.shen at oracle.com
Wed Sep 24 06:58:49 UTC 2014
java/time/Duration.java has the pattern for the duration, which Is similar to Guy's suggestion.
> On Sep 24, 2014, at 10:08 PM, Wang Weijun <weijun.wang at oracle.com> wrote:
>
>
>> On Sep 25, 2014, at 13:06, Guy Steele <guy.steele at oracle.com> wrote:
>>
>> (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).
>
> Yes, this is much better.
>
>>
>> But maybe that takes all the fun out of it.
>
> Let someone else enjoy it then. :-)
>
> Thanks
> Max
>
>>
>> --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