RE Match each word with a part of the pattern


I think this is best illustrated with an example.

I have the RE pattern choco_icecream = "(do|does) w+ (loves|love|likes|like) (choco|chocolate) (ice-cream|icecream|ice cream)".
I want to check if example = "Does Jessica like Chocolate Icecream".lower() is a match for choco_icecream.

The example is a match of choco_icecream, but I want to know which word of example corresponds to an element of choco_icecream.

I would like in a dictionary. The ideal output will look like this: {"(do|does)":"does", "w+":"jessica", "(loves|love|likes|like)":"like", "(choco|chocolate)":"chocolate", "(ice-cream|icecream|ice cream)":"icecream"}

How could I achieve this?

Asked By: user17598284



I think this would all have been clear if you had just played around with this a bit, along with the documentation.

import re

choco_icecream = "(do|does) (w+) (loves|love|likes|like) (choco|chocolate) (ice-cream|icecream|ice cream)"
example = "Does Jessica like Chocolate Icecream".lower()

x = re.match(choco_icecream, example)


<re.Match object; span=(0, 36), match='does jessica like chocolate icecream'>
('does', 'jessica', 'like', 'chocolate', 'icecream')

Printing the whole match object gives you the entire match, but every set of parentheses you have in your expression creates a new "group", and the groups are all returned individually.

Note that I modified your regex to make the name a group as well.

Answered By: Tim Roberts