简体   繁体   English

将嵌套元素与通配符匹配

[英]Matching nested elements with wildcard

I am trying to delete parts of a string with regex. 我试图用正则表达式删除字符串的一部分。 I want to delete all parts, which look like this: 我要删除所有部分,如下所示:

[[File: bla bla bla]]

so I used the following regular expression (note: [\\x5B] is '[' and [\\x5D] is ']'): 所以我使用了以下正则表达式(注意:[\\ x5B]为'[',[\\ x5D]为']'):

@"[\x5B][\x5B]File(.*?)[\x5D][\x5D]"
// Translates to @"[[File(.*?)]]"

However the text in the file tag can contain further tags, ie 但是,文件标签中的文本可以包含其他标签,即

[[File: bla bla [[foo]] bla]]

The above expression would leave 上面的表达式会离开

bla]]

because it detects the closing parentheses as the end of the match (note, I am searching non-greedy). 因为它检测到右括号是比赛的结尾(请注意,我搜索的是非贪婪的)。

I therefore came up with this: 因此,我想到了这一点:

@"[\x5B][\x5B]File(.*?)[\x5B][\x5B](.*?)[\x5D][\x5D](.*?)[\x5D][\x5D]"
// Translates to @"[[File(.*?)[[(.*?)]](.*?)]]" 

which deletes all File tags, containing one inner tag. 删除所有包含一个内部标签的文件标签。 First calling this regex and then the simpler one above would delete all with one and zero inner tags. 首先调用此正则表达式,然后调用上面的简单正则表达式将删除所有带有1和0内部标记的标记。

However, the File tag can contain an arbitrary number of inner tags and obviously, my approach isn't very nice. 但是,文件标签可以包含任意数量的内部标签,显然,我的方法不是很好。 I am just getting started with regex and any help is much appreciated. 我刚刚开始使用正则表达式,非常感谢您的帮助。

I think this is how it is in the books. 我认为书中就是这样。
@"\\[(?>(?:(?!\\[|\\]).)+|\\[(?<Depth>)|\\](?<-Depth>))*(?(Depth)(?!))\\]"

 \[                            # Match opening [
 (?>                           # Then either match (possessively):
      (?:                           #  the following group which matches
           (?! \[ | \] )                 #  (but only if we're not at the start of [ or ] )
           .                             #  any character
      )+                            #  once or more
   |                              # or
      \[                            #  [ (and increase the braces counter)
      (?<Depth> )
   |                              # or
      \]                            #  ] (and decrease the braces counter).
      (?<-Depth> )
 )*                            # Repeat as needed.
 (?(Depth)                     # Assert that the braces counter is at zero.
      (?!)                          # Fail this part if depth > 0
 )
 \]                            # Then match a closing ]. 

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

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