使用 C# 正则表达式嵌套括号

问题描述 投票:0回答:1

我想使用正则表达式模式从乳胶文本中提取分数。

我的代码:

string inputText = "frac{frac{1/2}}frac{frac{255/7890}/2}frac{frac{frac{ZZ/99}/66}/33}frac{frac{66/77}/frac{88/99}}";

string pattern = @"(frac{)(.\\\*?)(?\\\<=\\\[}\\\]{1,})";

var matches = Regex.Matches(inputText, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled).Cast\\\<Match\\\>().OrderBy(s =\\\> s.Index);

输出:

数量:5

frac{frac{1/2}

frac{frac{255/7890}

frac{frac{frac{ZZ/99}

frac{frac{66/77}

frac{88/99}

我想要这个输出:

数量:4

frac{frac{1/2}}

frac{frac{255/7890}/2}

frac{frac{frac{ZZ/99}/66}/33}

frac{frac{66/77}/frac{88/99}}
c# regex latex
1个回答
0
投票

你可以尝试一下:

        string inputText = "frac{frac{1/2}}frac{frac{255/7890}/2}frac{frac{frac{ZZ/99}/66}/33}frac{frac{66/77}/frac{88/99}}";

        string pattern = @"frac\{(?:[^{}]|(?<open>{)|(?<-open>}))*\}(?(open)(?!))";

        var matches = Regex.Matches(inputText, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled)
                            .Cast<Match>()
                            .OrderBy(s => s.Index);

        Console.WriteLine("Count: " + matches.Count());
        foreach (var match in matches)
        {
            Console.WriteLine(match);
        }

图案说明:

        // frac\{                - Match "frac{"
        // (?:                   - Start of non-capturing group
        //   [^{}]               - Match any character except "{" or "}"
        //   |                   - OR
        //   (?<open>{)          - Match "{", incrementing the "open" counter
        //   |                   - OR
        //   (?<-open>})         - Match "}", decrementing the "open" counter
        // )*                    - End of non-capturing group, repeat zero or more times
        // }                     - Match "}"
        // (?(open)(?!))         - Conditional to ensure all "{" are balanced, if "open" counter is not zero, fail

结果如下:

© www.soinside.com 2019 - 2024. All rights reserved.