Python regex: matching a parenthesis within parenthesis

前端 未结 6 1683
滥情空心
滥情空心 2020-12-14 16:52

I\'ve been trying to match the following string:

string = \"TEMPLATES = ( (\'index.html\', \'home\'), (\'base.html\', \'base\'))\"

But unfo

6条回答
  •  情话喂你
    2020-12-14 17:12

    If your strings look like valid Python code anyways you can do this:

    import ast
    var, s = [part.strip() for part in 
         "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))".split('=')]
    result= ast.literal_eval(s)
    

提交回复
热议问题