递归正则表达式:无法识别的分组构造

时间:2015-08-18 09:29:14

标签: c# .net regex

我已经编写了一个正则表达式来解析BibTex条目,但我认为我使用了.net中不允许的内容,因为我得到了Unrecognized grouping construct 异常。

有人能发现我的错误吗?

(?<entry>@(\w+)\{(\w+),(?<kvp>\W*([a-zA-Z]+) = \{(.+)\},)(?&kvp)*(\W*([a-zA-Z]+) = \{(.+)\})\W*\},?\s*)(?&entry)*

可以在https://regex101.com/r/uM0mV1/1

看到

2 个答案:

答案 0 :(得分:1)

这就是我捕获你提供的字符串中所有细节的方法:

@(?<type>\w+)\{(?<name>\w+),(?<kvps>\s*(?<attribute>\w+)\s*=\s*\{(?<value>.*?)},?\r?\n)+}

请参阅demo

这个正则表达式运行良好,因为C#正则表达式引擎将所有捕获的文本保存在堆栈中,并且可以通过Groups["name"].Captures属性访问它。

C#代码显示了如何使用它:

var pattern = @"@(?<type>\w+)\{(?<name>\w+),(?<kvps>\s*(?<attribute>\w+)\s*=\s*\{(?<value>.*?)},?\r?\n)+}";
var matches = Regex.Matches(line, pattern);
var cnt = 1;
foreach (Match m in matches)
{
    Console.WriteLine(string.Format("\nMatch {0}", cnt));
    Console.WriteLine(m.Groups["type"].Value);
    Console.WriteLine(m.Groups["name"].Value);
    for (int i = 0; i < m.Groups["attribute"].Captures.Count; i++)
    {
        Console.WriteLine(string.Format("{0} - {1}",
              m.Groups["attribute"].Captures[i].Value,
              m.Groups["value"].Captures[i].Value));
     }
     cnt++;
}

输出:

Match 1
article
Gettys90
author - Jim Gettys and Phil Karlton and Scott McGregor
abstract - A technical overview of the X11 functionality. This is an update of the X10 TOG paper by Scheifler \& Gettys.
journal - Software Practice and Experience
volume - 20
number - S2
title - The {X} Window System, Version 11
year - 1990

Match 2
article
Gettys90
author - Jim Gettys and Phil Karlton and Scott McGregor
abstract - A technical overview of the X11 functionality. This is an update of the X10 TOG paper by Scheifler \& Gettys.
journal - Software Practice and Experience
volume - 20
number - S2
title - The {X} Window System, Version 11
year - 1990

Match 3
article
Gettys90
author - Jim Gettys and Phil Karlton and Scott McGregor
abstract - A technical overview of the X11 functionality. This is an update of the X10 TOG paper by Scheifler \& Gettys.
journal - Software Practice and Experience
volume - 20
number - S2
title - The {X} Window System, Version 11
year - 1990

答案 1 :(得分:-1)

我认为你的命名反向引用是错误的。 See MSDN.请尝试以下

(?<entry>@(\w+)\{(\w+),(?<kvp>\W*([a-zA-Z]+) = \{(.+)\},)\k<kvp>*(\W*([a-zA-Z]+) = \{(.+)\})\W*\},?\s*)\k<entry>*