用于查找最外层序列的正则表达式

时间:2015-02-03 13:30:18

标签: c# regex

我特殊的html序列。我想匹配区域和目标foreach Blocks直到} @。子区域是嵌套序列,应排除在外。

@foreach(region){
    @foreach(subregion){
        @{subregion.name}
    }@
}@
@foreach(destination){
    @{destination.name}
}@

1 个答案:

答案 0 :(得分:1)

您需要使用平衡组:

@foreach\([^)]*\)\s*{(?>(?<open>@foreach\([^)]*\)\s*{)|@{[^}]*}|[^}@]+|(?<-open>}@))*(?(open)(?!))}@

这个想法是定义一个像计数器一样工作的命名捕获。当找到一个开放子字符串时,计数器会递增,当找到一个结束子字符串时,计数器会递减。如果计数器不为空,(?(open)(?!))会强制模式失败。

@foreach\([^)]*\)\s*{
(?> # possible content in a foreach
    (?<open>@foreach\([^)]*\)\s*{) # increment the counter
  |
    @{[^}]*} 
  |
    [^}@]+   # all that is not a @ or } (can be improved to be more flexible)
  |
    (?<-open>}@) # decrement the counter
)*
(?(open)     # conditional statement (if "open" is not null)
    (?!)     # then make fail the pattern (with the always false assertion `(?!)`)
)
}@

demo