Si nos échantillons se limitent à ceux de la liste, nous pouvons commencer par cette expression :
(«{[^»]+»|<<{[^>]+>>|{[^}]+})
Test avec re.finditer
import re
regex = r"(«{[^»]+»|<<{[^>]+>>|{[^}]+})"
test_str = (" sent1 = \"Do you want to eat «{Food}»? %[Y](A:y) %[N](A:n)\"\n"
" sent2 = \"You were drinking <<{coldBeverage}>>, do you want to drink <<{hotBeverage}>> instead?\"\n"
" sent3 = \"I am a {animal} who can talk.\"\n\n"
" re.findall(regex, sent1) = [\"«{Food}»\"]\n"
" re.findall(regex, sent2) = [\"<<{coldBeverage}>>\", \"<<{hotBeverage}>>\"]\n"
" re.findall(regex, sent3) = [\"{animal}\"]")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
Test avec re.findall
import re
regex = r"(«{[^»]+»|<<{[^>]+>>|{[^}]+})"
test_str = (" sent1 = \"Do you want to eat «{Food}»? %[Y](A:y) %[N](A:n)\"\n"
" sent2 = \"You were drinking <<{coldBeverage}>>, do you want to drink <<{hotBeverage}>> instead?\"\n"
" sent3 = \"I am a {animal} who can talk.\"\n\n"
" re.findall(regex, sent1) = [\"«{Food}»\"]\n"
" re.findall(regex, sent2) = [\"<<{coldBeverage}>>\", \"<<{hotBeverage}>>\"]\n"
" re.findall(regex, sent3) = [\"{animal}\"]")
print(re.findall(regex, test_str))
L'expression est expliquée sur le panneau supérieur droit de cette démo si vous souhaitez l'explorer/simplifier/modifier, et en ce lien Si vous le souhaitez, vous pouvez observer pas à pas comment il se comporte par rapport à certains échantillons d'entrée.