使用正则表达式的粗略解析实现:
import re
s = "Would you like [to get]|[having]|g[to have] responses to your questions sent [up to]|g[to]|[on] you via email ?" # pattern string
choice_groups = re.compile(r"((?:g?\[[^\]]+\]\|?)+)") # regex to get choice groups
choices = re.compile(r"(g?)\[([^\]]+)\]") # regex to extract choices within each group
# now, use the regexes to parse the string:
groups = choice_groups.findall(s)
# returns: ['[to get]|[having]|g[to have]', '[up to]|g[to]|[on]']
# parse each group to extract possible choices, along with if they are good
group_choices = [choices.findall(group) for group in groups]
# will contain [[('', 'to get'), ('', 'having'), ('g', 'to have')], [('', 'up to'), ('g', 'to'), ('', 'on')]]
# finally, substitute each choice group to form a template
template = choice_groups.sub('___', s)
# template is "Would you like ___ responses to your questions sent ___ you via email ?"
现在解析它以适合您的格式应该很容易。祝你好运 :)