.Net Regex - 需要帮助 [英] .Net Regex - Help needed

查看:70
本文介绍了.Net Regex - 需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新了



我需要一个能找到@ [.. [...]]模式的正则表达式的帮助。



我会尝试解释。

------------------- ---

文本将包含占位符,在显示相同文本时将替换为值。



占位符有3部分;

- 一个开放标签,以@ [后跟点分隔文本开头,以[,

- 属性列表结尾,带有qouted(double qoutes)值的逗号分隔列表,

- 关闭标记,]]。



该属性列表项可以包含一个或多个占位符(嵌套)和双qoutes(转义)和括号。



正则表达式必须通过知道何时它来克服嵌套占位符的问题到达外部占位符的末尾以及任何逃脱的qoutes和括号。



样本

------

考虑以下文本片段:

Updated

I need help with a regex that will find a "@[..[...]]" pattern.

I will try to explain.
----------------------
A text will contain placeholders which will be replaced with values upon display of that very same text.

A place holder has 3 parts;
- an open tag, starts with "@[" followed by "a dot delimited text" and ends with "[",
- a property list, a "comma separated list" with qouted (double qoutes) values,
- a close tag, "]]".

The property list items can contain one or many placeholders (nested) and both double qoutes (escaped) and brackets.

The regex must overcome the issues with nested placeholders by knowing when it reached the end of the "outer" placeholder as well as any escaped qoutes and brackets.

Sample
------
Consider the following text fragment:

Linklist
@[Link.AppText["[startpage]", "startpage"]]
@[Link.Text["[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"]]
@[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]





文本片段匹配应如下所示:



The text fragment match should look like this:

match 1  =  @[Link.AppText["[startpage]", "startpage"]]
   Gr.1  =  Link.AppText
   Gr.2  =  "[startpage]", "startpage"

match 2  =  @[Link.Text["[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"]]
   Gr.1  =  Link.Text
   Gr.2  =  "[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"

match 3  =  @[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]
   Gr.1  =  Link.Text
   Gr.2  =  "@[Link["startpage"]]", "@[Text.AppText["startpage"]]"





有建议(来自另一个c的@ridgerunner) ommunity)我走到这一步:



With suggestion (by @ridgerunner from another community) I came this far:

@\[([._\w]+)\[([^[\]""]*(?:""[^""]*""[^[\]""]*)*)\]\]

@\[                                # Outer open delimiter.
([._\w]+)                          # 1:st group.
\[                                 # Inner open delimiter.
(                                  # Start of 2:nd group.
[^[\]""]*(?:""[^""]*""[^[\]""]*)*  # Contents.
)                                  # End of 2:nd group.
\]\]                               # Close delimiter.





这给出了以下结果



Which gives the following result

match 1  =  @[Link.AppText["[startpage]", "startpage"]]
   Gr.1  =  Link.AppText
   Gr.2  =  "[startpage]", "startpage"

match 2  =  @[Text.AppText["startpage"]]
   Gr.1  =  Text.AppText
   Gr.2  =  "startpage"

match 3  =  @[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]
   Gr.1  =  Link.Text
   Gr.2  =  "@[Link["startpage"]]", "@[Text.AppText["startpage"]]"





正如您所看到的,它与想要的结果不符。第2场比赛错了。



但是,如果我改变了逃脱的qoutes(从\到),我得到了这个结果:



As you can see it doesn't match the wanted result. Match 2 is wrong.

BUT, if I change the escaped qoutes (from \" to "") I get this result:

match 1  =  @[Link.AppText["[startpage]", "startpage"]]
   Gr.1  =  Link.AppText
   Gr.2  =  "[startpage]", "startpage"

match 2  =  @[Link.Text["[startpage] loggedin", "The ""@[Text.AppText["startpage"]]"" for users"]]
   Gr.1  =  Link.Text
   Gr.2  =  "[startpage] loggedin", "The ""@[Text.AppText["startpage"]]"" for users"

match 3  =  @[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]
   Gr.1  =  Link.Text
   Gr.2  =  "@[Link["startpage"]]", "@[Text.AppText["startpage"]]"





剩下要解决的是如何让它与逃逸和加倍的qoutes一起工作。



正如所讨论的与谢尔盖(见下文),我更新了这个问题,当然,正则表达式和解析器的组合可以使事情更安全..和.Net的平衡组m也是一个替代方案。



Left to solve is how to make it work with both "escaped" and "doubled" qoutes.

As discussed with Sergey (see below), I updated this question and of course a combination of regex and a parser could make things more fail-safe .. and .Net's "Balanced groups" might be an alternative as well.

推荐答案

在谷歌搜索和阅读关于平衡组的努力之后我终于开始工作,尽管我不得不稍微改变模式它起作用,至少对我而言:)

After struggling with google search and reading about balanced group I finally got things working, though I had to alter the pattern slightly to make it work, at least for me :)
Regex:  @([._\w]+)\[\[""((?:[^\[\]]*|\[[^\[]|[^\]]\]|(?<counter>\[\[)|(?<-counter>\]\]))+(?(counter)(?!)))""\]\]

@([._\w]+)\[\[""          #   start tag, 1:st group
  (                       #   start 2:nd group
    (?:                   #   non capturing group
      [^\[\]]*            #   any char but [ or ]
      |                   #   or
      \[[^\[]             #   if [, not followed by a [
      |                   #   or
      [^\]]\]             #   if ], not followed by a ]
      |                   #   or
      (?<counter>\[\[)    #   counter start tag
      |                   #   or
      (?<-counter>\]\])   #   counter stop tag
    )+                    #   end non capturing group
    (?(counter)(?!))      #   if counter <> 0, regex fails
  )                       #   end 2:nd group
"\]\]                     #   end tag



用新模式更新占位符; (@ .. [[...]]


Updated placeholders with new pattern; (@..[[...]]

Linklist
@Link.AppText[["[startpage]", "startpage"]]
@Link.Text[["[startpage] loggedin", "The "@Text.AppText[["startpage"]]" for users"]]
@Link.Text[["@Link[["startpage"]]", "@Text.AppText[["startpage"]]"]]



这给了我我想要的确切内容:


Which gives me exactly want I want:

match 1
   Gr.1   Link.AppText
   Gr.2   [startpage]", "startpage

match 2
   Gr.1   Link.Text
   Gr.2   [startpage] loggedin", "The "@Text.AppText[["startpage"]]" for users

match 3
   Gr.1   Link.Text
   Gr.2   @Link[["startpage"]]", "@Text.AppText[["startpage"]]


这篇关于.Net Regex - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆