如何在允许嵌套模式的同时将我的整个字符串与正则表达式匹配

时间:2019-02-01 09:21:28

标签: regex balanced-groups

我当前的正则表达式仅匹配嵌套模式,而不匹配最外面的@if .... endif @模式。如何修改表达式以匹配最外面的模式。如果从字符串中删除了最外面的模式,则表达式应该与所有嵌套模式之前的模式匹配。 许多或或图案只有在现实世界有关。

正则表达式:

(?<match>
@if\((?<statement>[^)]*)\){(?<ifblock>(?>
(?<open>@if\([^)]*\){)
    |<script[^>]+>.*?</script>
    |(?<valuetag>\@{(?<tag>[^}]+)})(?:(?:(\.|\$))(?<action>[^)@]+[)]))?
    |(?<configtag>@(?:(?<tagtype>cc|dc|sc|wc)){(?<tag>[^}\.]*)(?:(?:\.)(?<action>(?:ToLower|ToUpper|MaxLength)))?}(?:cc|dc|sc|wc)@)
    |(?<match>@weeknumber{(?<date>((?<valuetag>\@{(?<tag>[^}]+)})(?:(?:(\.|\$))(?<action>[^)@]+[)]))?|((0[1-9]|[12][0-9]|3[01])[-](0[1-9]|1[012])[-](19|20)\d\d)))}weeknumber@)
    |(?<tag>\@{(?<tagname>[^}]+)}[.]ReplaceText\((?<replace>[^;]+)[;]{1}(?<with>[^)]+)\))
    |(?<match>@(block|if)\((?<statement>[^)]*)\){(?<ifblock>.*)(?=)}end(block|if)@)
    |[^}@]+
    |@{value(\.[A-Z][0-9])?}
    |(?<-open>}endif@)
)*)
(?(open)(?!))
(
}else{(?<elseblock>(?>
(?<open>@if\([^)]*\){)
    |<script[^>]+>.*?</script>
    |(?<valuetag>\@{(?<tag>[^}]+)})(?:(?:(\.|\$))(?<action>[^)@]+[)]))?
    |(?<configtag>@(?:(?<tagtype>cc|dc|sc|wc)){(?<tag>[^}\.]*)(?:(?:\.)(?<action>(?:ToLower|ToUpper|MaxLength)))?}(?:cc|dc|sc|wc)@)
    |(?<match>@weeknumber{(?<date>((?<valuetag>\@{(?<tag>[^}]+)})(?:(?:(\.|\$))(?<action>[^)@]+[)]))?|((0[1-9]|[12][0-9]|3[01])[-](0[1-9]|1[012])[-](19|20)\d\d)))}weeknumber@)
    |(?<tag>\@{(?<tagname>[^}]+)}[.]ReplaceText\((?<replace>[^;]+)[;]{1}(?<with>[^)]+)\))
    |(?<match>@(block|if)\((?<statement>[^)]*)\){(?<ifblock>.*)(?=)}end(block|if)@)
    |[^}@]+
    |@{value(\.[A-Z][0-9])?}
    |(?<-open>}endif@)
)*)
(?(open)(?!))
)?
}endif@)

字符串:

        @if(base.SiteBID=='SOS'){
    <div class="floatleft reviewcontainer_outer" onclick="@if(base.IsSmartPhone=='False'){toogleReviewModalLightbox('@{house.id}', 0, 10);}endif@">
        @if(house.latestreview.amount<greaterthen>2){<div class="sos_sprite reviews@{house.category} sos_inline onlinecat_review_gfx@{house.latestreview.totalrankcss} floatleft reviewcontainer_sprite" title="@{house.latestreview.totalrank}"></div><p class="broedtekst floatleft link">@{house.latestreview.amount} @cc{txt_guestbook_result}cc@</p>}endif@
    </div>
                            @if(base.IsSmartPhone=='False'){toogleReviewModalLightbox('@{house.id}', 0, 10);}else{jhfr}endif@
    }endif@

我希望表达式匹配从@if到endif @的整个字符串

1 个答案:

答案 0 :(得分:0)

我尝试了这些,并且它们起作用了

(@if.+\\n+\\D+.*\\W+.*\\B.*\\n.*\\n.*\\n.*endif@)

下面的这个看起来较短。

(@if.+\\n+\\D+.*\\W+.*\\B.*\\n.*\\n.*\\n.*)

两个工作。 在regex101.com上进行了测试