Matching balanced parenthesis in Perl regex

前端 未结 6 1633
后悔当初
后悔当初 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:35

    Although Recursive Regular Expressions can usually be used to capture "balanced braces" {}, they won't work for you, because you ALSO have the requirement to match "balanced quotes" ".
    This would be a very tricky task for a Perl Regular Expression, and I'm fairly certain it's not possible. (In contrast, it could probably be done with Microsoft's "balancing groups" Regex feature).

    I would suggest creating your own parser. As you process each character, you count each " and {}, and only split on , if they are "balanced".

提交回复
热议问题