gpt4 book ai didi

c# - 递归正则表达式 : Unrecognized grouping construct

转载 作者:太空狗 更新时间:2023-10-29 22:00:32 25 4
gpt4 key购买 nike

我已经编写了一个正则表达式来解析 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)+}

参见 demo

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

显示如何使用它的 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

关于c# - 递归正则表达式 : Unrecognized grouping construct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32068580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com