DEV Community

Discussion on: Advent of Code 2020: Day 02(b) using regex named groups

Collapse
 
spark706 profile image
spark706

When I read in the Named Groups into Python 3.7, I get an error: TypeError: unsupported operand type(s) for &: 'str' and 'int'
Any guidance with this error?

Collapse
 
meseta profile image
Yuan Gao

that doesn't seem like a named groups error, it seems like you've tried to & a string and an int at some point. Check that your types are all correct

Collapse
 
spark706 profile image
spark706

I'm not sure where the & is introduced. I even tried your code copy and pasted into the IDE. But what I did notice and change to get the code working is used re.match instead of re.compile. Not sure why, still early in learning Python, but the following worked:

pattern = re.match('(?P\d+)-(?P\d+) (?P\w): (?P\w+)', entry)
print(pattern.groupdict())

Thanks for the help and this series. Even on Day 2 and it's been extremely helpful.