我想使用正则表达式模式从乳胶文本中提取分数。
我的代码:
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}}
你可以尝试一下:
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