正则表达式,匹配包含相同字符的两个字符之间的字符串来分隔

时间:2014-02-23 13:23:03

标签: regex regex-greedy

我认为这个标题可能听起来很模糊,但请继续阅读以理解我的意思。

假设我有这个字符串:

TestBlock1 {    NestedBlock1.1    {
      Text = Text    } }
TestBlock2 {    NestedBlock2.1    {
      Text = Text    }    NestedBlock2.2    {
      Text = Text    } }

我希望能够通过BlockName {...}匹配字符串。这就是我的尝试:

[\w]+\s*{\s*[^.]+\s*}

我们的想法是将匹配项放入数组中。

string[] block;
block[0] = TestBlock1 { NestedBlock1.1 { Text = Text } }
block[1] = TestBlock2 { NestedBlock2.1 { Text = Text } NestedBlock2.2 { Text = Text } }

问题在于它获取整个字符串。甚至可以在两个也包含“分隔符”字符的字符之间获取字符串吗?

1 个答案:

答案 0 :(得分:0)

在.NET(支持递归正则表达式)中,您可以使用

Regex regexObj = new Regex(
    @"\w+\s+        # Match identifier
    \{              # Match {
    (?>             # Then either match (possessively):
     (?:            # the following group which matches
      (?![{}])      # only if we're not before a { or }
      .             # any character
     )+             # once or more
    |               # or
     \{ (?<Depth>)  # { (and increase the braces counter)
    |               # or
     \} (?<-Depth>) # } (and decrease the braces counter).
    )*              # Repeat as needed.
    (?(Depth)(?!))  # Assert that the braces counter is at zero.
    \}              # Then match }.", 
    RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);