【问题标题】:Recursive regex: Unrecognized grouping construct递归正则表达式:无法识别的分组构造
【发布时间】:2015-08-18 09:29:14
【问题描述】:

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

【问题讨论】:

  • 哦,不支持使用(?&amp;...) 递归命名子模式。
  • 我可以使用类似的东西吗?
  • 只需声明一个变量,然后使用string.Format 构建正则表达式。
  • 我看不出这将如何解决我的问题..
  • 您正试图通过重复使用模式部分和递归命名子模式来缩短正则表达式模式。对?你不能在 .NET 中做到这一点。 “动态”构建正则表达式。我现在将尝试说明它。这是what I mean。但是,我认为您的正则表达式存在一些问题。是否要匹配所有单个条目?

标签: c# .net regex


【解决方案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

【讨论】:

    【解决方案2】:

    我认为您命名的反向引用是错误的。 See MSDN.尝试关注

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

    【讨论】:

    • 只有当递归与第一个递归相同时,\k 关键字才匹配(据我所知)(regex101.com/r/aZ7xE3/1
    • 你是对的。如果重复不应该相同,那就更容易了。你不需要反向引用。改用量词+(?&lt;entry&gt;@(\w+)\{(\w+),(?&lt;kvp&gt;\W*([a-zA-Z]+) = \{(.+)\},)+(\W*([a-zA-Z]+) = \{(.+)\})\W*\},?\s*)+
    • 这也行不通,因为它只会生成一个匹配项,因此难以提取数据。 (在我看来)。并且大部分数据将在一个无与伦比的组中。 regex101.com/r/pU6jM6/1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2018-06-16
    相关资源
    最近更新 更多