我有一个字符串,我想将其拆分为特定的段,但由于两次出现相同的模式,我无法匹配字符串的正确段。
我的字符串:
@if(text.text isempty){<customer_comment>@cc{txt_without_comments}cc@</customer_comment>}else{@if(text.answer=='no'){<customer_comment>@{text.text}</customer_comment>}else{<answer>@{text.text}</answer>}endif@}endif@
我需要匹配:@if(text.text isempty){@cc{txt_without_comments}cc@}else{....}endif@
而不是 else 块中的嵌套点。
这是我不完整的正则表达式:
(?<match>(?<open>@if\((?<statement>[^)]*)\)\s*{)(?<ifblock>(.+?)(?:}else{)(?<elseblock>.*))(?<-open>)}endif@)
这个正则表达式在 ifblock 组中过于贪婪,它应该在第一个 }else{ 模式处停止。
编辑:这是我想要产生的确切结果:
match: @if(text.text isempty){<customer_comment>@cc{txt_without_comments}cc@</customer_comment>}else{@if(text.answer=='no'){<customer_comment>@{text.text}</customer_comment>}else{<answer>@{text.text}</answer>}endif@}endif@
statement: text.text isempty
ifblock: <customer_comment>@cc{txt_without_comments}cc@</customer_comment>
elseblock: @if(text.answer=='no'){<customer_comment>@{text.text}</customer_comment>}else{<answer>@{text.text}</answer>}endif@