Python - Multiple choice markup parsing
Question: Consider this text:

> Would you like to have responses to your questions sent to you via email ?

I'm going to propose multiple choices for several words by marking up them
like that:

> Would you like [to get]|[having]|g[to have] responses to your questions sent
> [up to]|g[to]|[on] you via email ?

The choices are **bracketted** and separated by **pipes**  
The good choice is preceded by a **g**

I would like to parse this sentence to get the text formatted like that:

> Would you like __ responses to your questions sent __ you via email ?

With a list like:

    
    
    [
      [
        {"to get":0},
        {"having":0},
        {"to have":1},
      ],
      [
        {"up to":0},
        {"to":1},
        {"on":0},
      ],
    ]
    

Is my markup design ok ?  
How to regex the sentence to get the needed result and generate the list ?

**edit** : User oriented markup language needed


Answer: I would add some grouping parentheses `{}`, and output not list of list of
dicts, but list of dicts.

Code:

    
    
    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 ?'
    
    def variants_to_dict(variants):
        dct = {}
        for is_good, s in variants:
            dct[s] = 1 if is_good == 'g' else 0
        return dct
    
    def question_to_choices(s):
        choices_re = re.compile(r'{[^}]+}')
        variants_re = re.compile(r'''\|?(g?)
                                     \[
                                        ([^\]]+)
                                     \]
                                    ''', re.VERBOSE)
        choices_list = []
        for choices in choices_re.findall(s):
            choices_list.append(variants_to_dict(variants_re.findall(choices)))
    
        return choices_re.sub('___', s), choices_list
    
    question, choices = question_to_choices(s)
    print question
    print choices
    

Output:

    
    
    Would you like ___ responses to your questions sent ___ you via email ?
    [{'to have': 1, 'to get': 0, 'having': 0}, {'to': 1, 'up to': 0, 'on': 0}]
    

