繁体   English   中英

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

[英]Recursive regex: Unrecognized grouping construct

我编写了一个正则表达式来解析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上看到

这就是我如何捕获您提供的字符串中的所有细节:

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

演示

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

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

我假设您的命名反向引用是错误的。 请参阅MSDN。 试试以下

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

暂无
暂无

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

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