Match.groupdict: Meaning of default argument?

Loris Bennett loris.bennett at fu-berlin.de
Tue May 3 07:49:47 EDT 2022


"Loris Bennett" <loris.bennett at fu-berlin.de> writes:

> ram at zedat.fu-berlin.de (Stefan Ram) writes:
>
>> "Loris Bennett" <loris.bennett at fu-berlin.de> writes:
>>>I thought that 'days' would default to '0'.
>>
>>   It will get the value '0' if (?P<days>\d*) does 
>>   /not/ participate in the match.
>>
>>   In your case, it /does/ participate in the match,
>>   \d* matching the empty string.
>>
>>   Try (?P<days>\d+)?.
>
> Ah, thanks.  I was misunderstanding the meaning of 'participate'.

What I actually need is

  ((?P<days>\d+)(-?))?(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)

so that I can match both

  99-11:22:33

and 

  11:22:33

and have 'days' be '0' in the later case.
 
Thanks for pointing me in the right direction.

Cheers,

Loris

-- 
This signature is currently under construction.


More information about the Python-list mailing list