递归正则表达式:无法识别的分组结构
本文关键字:结构 识别 正则表达式 递归 | 更新日期: 2023-09-27 18:08:23
我已经写了一个正则表达式来解析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>*