简体   繁体   English

.NET正则表达式如何

[英].NET Regular expression how to

New to Regex. 正则表达式的新功能。 I want to validate to this format: 我想验证这种格式:

  • Any character allowed, except '{' and '}'. 允许使用任何字符,“{”和“}”除外。
  • A '{' char must be followed by one of specific strings '{'char必须后跟一个特定的字符串

After these strings any character can go 在这些字符串后,任何角色都可以去

  • Each '{' must have a closing '}' 每个'{'必须有一个结束'}'
  • Nesting of '{'s is allowed 允许嵌套'{'s

Example: 例:

abc{FILE:any text} def {FILE:mno{ENV:xyz}}

FILE: and ENV: are an example of specific strings required after a '{' char. FILE:ENV:是'{'char之后所需的特定字符串的示例。 I wrote this regex: 我写了这个正则表达式:

^
(
  [^\{\}]+
  |
  (?<Depth>\{)(FILE:|ENV:)
  |
  (<-Depth>\})
)*
(?(Depth)(?!))
$

but it doesn't match my desired format. 但它与我想要的格式不符。 What i miss? 我想念的是什么?
Thanks a lot. 非常感谢。
EDIT: Links that do the same, succesfully i hope:-) MSDN , Other site 编辑:链接,做同样的,我希望成功:-) MSDN其他网站

You forgot the question mark in the balancing group. 你忘记了平衡组中的问号。

string pattern = @"(?x)
^
(?:
    [^{}]+
    |
    (?<Depth>{) (?:FILE|ENV):
    |
    (?<-Depth>})
)*
(?(Depth)(?!))
$
";

Should match strings like a {FILE: {ENV: foo } bar } baz 应匹配a {FILE: {ENV: foo } bar } baza {FILE: {ENV: foo } bar } baz字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM