import re


def get_dict(post, key):
    """ Extract from POST PHP-like arrays as dictionary.

        Example usage::

            <input type="text" name="option[key1]" value="Val 1">
            <input type="text" name="option[key2]" value="Val 2">

            options = get_dict(request.POST, 'option')
            options['key1'] is 'Val 1'
            options['key2'] is 'Val 2'
    """
    result = {}
    if post:
        patt = re.compile('^([a-zA-Z_]\w+)\[([a-zA-Z_\-][\w\-]*)\]$')
        for post_name, value in post.items():
            value = post[post_name]
            match = patt.match(post_name)
            if not match or not value:
                continue
            name = match.group(1)
            if name == key:
                k = match.group(2)
                result.update({k: value})
    return result