0

我有这样的文字:

This is {name1:value1}{name2:{name3:even dipper {name4:valu4} dipper} some inner text} text

我想解析出这样的数据:

Name: name1
Value: value1

Name: name2
Value: {name3:even dipper {name4:valu4} dipper} some inner text

然后我将递归处理每个值以解析出嵌套字段。你能推荐一个正则表达式来做到这一点吗?

4

2 回答 2

3

在 C# 中,您可以使用平衡组来计算和平衡括号:

{ (?'name' \w+ ) :       # start of tag
(?'value'                # named capture
  (?>                    # don't backtrack
    (?:
      [^{}]+             # not brackets
    | (?'open' { )       # count opening bracket
    | (?'close-open' } ) # subtract closing bracket (matches only if open count > 0)
    )*
  )
  (?(open)(?!))          # make sure open is not > 0
)
}                        # end of tag

示例

string re = @"(?x)       # enable eXtended mode (comments/spaces ignored)
{ (?'name' \w+ ) :       # start of tag
(?'value'                # named capture
  (?>                    # don't backtrack
    (?:
      [^{}]+             # not brackets
    | (?'open' { )       # count opening bracket
    | (?'close-open' } ) # subtract closing bracket (matches only if open count > 0)
    )*
  )
  (?(open)(?!))          # make sure open is not > 0
)
}                        # end of tag
";

string str = @"This is {name1:value1}{name2:{name3:even dipper {name4:valu4} dipper} some inner text} text";

foreach (Match m in Regex.Matches(str, re))
{
    Console.WriteLine("name: {0}, value: {1}", m.Groups["name"], m.Groups["value"]);
}

输出:

name: name1, value: value1
name: name2, value: {name3:even dipper {name4:valu4} dipper} some inner text
于 2013-03-11T19:52:43.040 回答
2

如果使用 Perl/PHP/PCRE,它一点也不复杂。您可以使用如下表达式:

{(\w+):         # start of tag
   ((?:
      [^{}]+    # not a tag
   |  (?R)      # a tag (recurse to match the whole regex)
   )*)
}               # end of tag
于 2013-03-11T15:47:28.560 回答