return inverse string selection in python
Question: I have a python snippet that returns the contents within two strings using
regex.

    
    
    res = re.search(r'Presets = {(.*)Version = 1,', data, re.DOTALL)
    

What I now want to do is return the two strings surrounding this inner part.
Keep in mind this is a multiline string. How can I get the bordering strings,
the beginning and end part in a two part list would be ideal.

    
    
    data = """{
        data = {
            friends = {
                max = 0 0,
                min = 0 0,
            },
            family = {
                cars = {
                    van = "honda",
                    car = "ford",
                    bike = "trek",
                },
                presets = {
                    location = "italy",
                    size = 10,
                    travelers = False,
                },
                version = 1,
            },
        },
        stuff = {
            this = "great",
        },
    }"""
    
    import re
    
    
    res = re.search(r'presets = {(.*)version = 1,', data, re.DOTALL)
    print res.groups(1)
    

In this case I would want to return the beginning string:

    
    
    data = """{
        data = {
            friends = {
                max = 0 0,
                min = 0 0,
            },
            family = {
                cars = {
                    van = "honda",
                    car = "ford",
                    bike = "trek",
                },
    

And the end string:

    
    
            },
        },
        stuff = {
            this = "great",
        },
    }"""
    


Answer: Regex is really not a good tool for parsing these strings, but you can use
`re.split` to achieve what you wanted. It can even combine the 2 tasks into
one:

    
    
    begin, middle, end = re.split(r'presets = \{(.*)version = 1,', data,
        flags=re.DOTALL)
    

`re.split` splits the string at matching positions; ordinarily the separator
is not in the resulting list. However, if the regular expression contains
capturing groups, then the matching contents of the first group is returned in
the place of the delimiter.

