Matching balanced parenthesis in Perl regex

前端 未结 6 1638
后悔当初
后悔当初 2021-01-12 08:52

I have an expression which I need to split and store in an array:

aaa=\"bbb{ccc}ffffd\" { aa=\"bb,cc\" { a=\"b\", c=\"d\" } }, aaa=\"bbb{}\" { aa=\"b}b\" }, aa         


        
6条回答
  •  孤街浪徒
    2021-01-12 09:33

    There's an example in perlre, using the recursive regex features introduced in v5.10. Although you are limited to v5.8, other people coming to this question should get the right solution :)

    $re = qr{ 
                (                                # paren group 1 (full function)
                    foo
                    (                            # paren group 2 (parens)
                        \(
                            (                    # paren group 3 (contents of parens)
                                (?:
                                    (?> [^()]+ ) # Non-parens without backtracking
                                    |
                                    (?2)         # Recurse to start of paren group 2
                                )*
                            )
                        \)
                    )
                )
        }x;
    

提交回复
热议问题