是否可以?匹配完全相同的开盘次数 &大括号 [英] Is it possible? Matching the exact same number of opening & closing braces
问题描述
我不是正则表达式方面的专家.我正在尝试创建一个正则表达式来匹配完全相同数量的左大括号和右大括号,但我不知道如何去做.
I'm no expert on Regex. I'm trying to create a regular expression that will match the exact same number of opening and closing braces, but I'm stumped on how to do it.
示例:
nothing: important, a b { c {{{ a another {{ nothing }} }}} }
或:
one { two {{ error, forgot ending brace }}
问题是我不知道我会事先得到多少个牙套.在第一个示例中,我需要获取文本 { c {{{ a another {{ nothing }} }}} }
.在第二个我需要得到 {{ 错误,忘记结束大括号 }}
.
The problem is that I don't know how many braces I'm going to get beforehand. In the first example, I need to get the text { c {{{ a another {{ nothing }} }}} }
. In the second I need to get {{ error, forgot ending brace }}
.
是否有可能创建一个匹配规则来做到这一点?(我使用的是 Qt 正则表达式引擎).
Is it even possible to create a matching rule that will do this? (I'm using Qt Regex engine).
推荐答案
这个问题用正则文法无法解决=>正则表达式无法解决.
This problem cannot be solved using regular grammar => it cannot be done by regular expression.
然而 - 根据 Dirk Vollmar 的评论 - 某些正则表达式引擎具有扩展功能,例如.NET 一个,它能够使用所谓的 "平衡组定义".
However - from Dirk Vollmar's comment - certain regex engines have extended functionality, e.g. the .NET one, which is capable of matching brackets using so called "balanced group definitions".
这篇关于是否可以?匹配完全相同的开盘次数 &大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!