使用正则表达式平衡组来匹配嵌套标签
using regular expression balancing groups to match nesting tags
我正在尝试使用正则表达式平衡组来匹配嵌套标签,如下所示:
some text ...
{list}
nesting loop content
{list}
{list}
{list}
bala ...
{/list}
{/list}
{/list}
{/list}
end
我的表情:
\{(?<NAME>.+?)\}
[^\{\}]*
(
(
\{(?<NAME2>.+?)\}(?<OPEN>)
[^\{\}]*?
)
(
\{\/\<NAME2>\}(?<-OPEN>)
[^\{\}]*?
)
)*
(?(OPEN)(?!))
\{\/\<NAME>\}
我的问题:
only last 2 level pair can match.
通常,要匹配嵌套标签,您需要类似于:
(?>
\{(?<Open>\w+)\}
|
\{/(?<-Open>\<Open>)\}
|
(?(Open)[^{}]+)
)*
(?(Open)(?!))
通过这种方式,您可以匹配 不同类型 的嵌套标签,这看起来就像您正在尝试做的那样。例如,它将匹配:
{list}
nesting loop content
{world}
{list}
{hello}
bala ...
{/hello}
{/list}
{/world}
{/list}
备注:
- 我正在使用
(?(Open)[^{}]+)
,所以我们只匹配标签内的自由文本。
- 我对顶层和内部层使用相同的组。
你的非常接近。你基本上错过了中间组之间的一个交替:
(
\{(?<NAME2>.+?)\}(?<OPEN>)
[^\{\}]*?
)
| # <---- This
(
\{\/\<NAME2>\}(?<-OPEN>)
[^\{\}]*?
)
但是,您总是使用 $NAME2
的 最后一个值。 $NAME2
是一个堆栈,但您永远不会从中弹出值,只能压入。这会导致一个错误:它也会匹配这个字符串(这可能是错误的):
{list} # Set $Name = "world"
nesting loop content
{world} # Set $Name2 = "world"
{world} # Set $Name2 = "world"
{hello} # Set $Name2 = "hello"
bala ...
{/hello} # Match $Name2 ("hello")
{/hello} # Match $Name2 ("hello")
{/hello} # Match $Name2 ("hello")
{/list} # Match $Name ("list")
另请参阅:
- What are regular expression Balancing Groups?
- How to make balancing group capturing?
我正在尝试使用正则表达式平衡组来匹配嵌套标签,如下所示:
some text ...
{list}
nesting loop content
{list}
{list}
{list}
bala ...
{/list}
{/list}
{/list}
{/list}
end
我的表情:
\{(?<NAME>.+?)\}
[^\{\}]*
(
(
\{(?<NAME2>.+?)\}(?<OPEN>)
[^\{\}]*?
)
(
\{\/\<NAME2>\}(?<-OPEN>)
[^\{\}]*?
)
)*
(?(OPEN)(?!))
\{\/\<NAME>\}
我的问题:
only last 2 level pair can match.
通常,要匹配嵌套标签,您需要类似于:
(?>
\{(?<Open>\w+)\}
|
\{/(?<-Open>\<Open>)\}
|
(?(Open)[^{}]+)
)*
(?(Open)(?!))
通过这种方式,您可以匹配 不同类型 的嵌套标签,这看起来就像您正在尝试做的那样。例如,它将匹配:
{list}
nesting loop content
{world}
{list}
{hello}
bala ...
{/hello}
{/list}
{/world}
{/list}
备注:
- 我正在使用
(?(Open)[^{}]+)
,所以我们只匹配标签内的自由文本。 - 我对顶层和内部层使用相同的组。
你的非常接近。你基本上错过了中间组之间的一个交替:
(
\{(?<NAME2>.+?)\}(?<OPEN>)
[^\{\}]*?
)
| # <---- This
(
\{\/\<NAME2>\}(?<-OPEN>)
[^\{\}]*?
)
但是,您总是使用 $NAME2
的 最后一个值。 $NAME2
是一个堆栈,但您永远不会从中弹出值,只能压入。这会导致一个错误:它也会匹配这个字符串(这可能是错误的):
{list} # Set $Name = "world"
nesting loop content
{world} # Set $Name2 = "world"
{world} # Set $Name2 = "world"
{hello} # Set $Name2 = "hello"
bala ...
{/hello} # Match $Name2 ("hello")
{/hello} # Match $Name2 ("hello")
{/hello} # Match $Name2 ("hello")
{/list} # Match $Name ("list")
另请参阅:
- What are regular expression Balancing Groups?
- How to make balancing group capturing?