Match.groupdict: Meaning of default argument?

Loris Bennett loris.bennett at fu-berlin.de
Fri Apr 29 03:49:47 EDT 2022


Hi,

If I do

  import re
  pattern = re.compile(r'(?P<days>\d*)(-?)(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)')
  s = '104-02:47:06'
  match = pattern.search(s)
  match_dict = match.groupdict('0')

I get 

  match_dict
  {'days': '104', 'hours': '02', 'minutes': '47', 'seconds': '06'}

However, if the string has no initial part (corresponding to the number of
days), e.g.

  s = '02:47:06'
  match = pattern.search(s)
  match_dict = match.groupdict('0')

I get

  match_dict
  {'days': '', 'hours': '02', 'minutes': '47', 'seconds': '06'}

I thought that 'days' would default to '0'.

What am I doing wrong?

Cheers,

Loris
-- 
This signature is currently under construction.


More information about the Python-list mailing list