pythonregex

Comma separated names list to parse into dictionary


I'd like to parse a list of assignees into a dictionary field. Creating the dictionary is not a problem since Python re.match() returned value can be grouped into dictionary fields re.match().groupdict().

My regex r'assignees:((\s\w+){2}[\s|,])+' matches as expected with the following example input if I test it in this website:

Input:

assignees: Peter Jones, Michael Jackson, Tim Burton

However, the same regex pattern in Python only matches Peter Jones, resulting in:

assignees: Peter Jones,

My pattern is declared in a variable p in this way:

p = re.compile(r'assignees:(?P<assignees>((\s\w+){2}[\s|,])+')

The final output I'm aiming for is:

{'assignees': 'Peter Jones, Michael Jackson, Tim Burton'}

Can anyone point out what I'm doing wrong/missing here please?


Solution

  • Your pattern was missing a parentheses and the question has no MRE, but this gives the result I believe you want. If it isn't satisfactory, make a MRE next time:

    import re
    
    s = 'assignees: Peter Jones, Michael Jackson, Tim Burton'
    p = re.compile(r'assignees:\s*(?P<assignees>([^,]+(?:,[^,]+)*))')
    m = p.match(s)
    print(m.groupdict())
    

    Output:

    {'assignees': 'Peter Jones, Michael Jackson, Tim Burton'}