示例文本:
heading1
foo=bar
baz=qux
heading2
spam=ham
heading3
this=that
我希望文本从heading1
到heading2
(不包括).
所以.com/r/yljACG/3" rel="nofollow noreferrer">I tried是从"heading1"
到"\n\n"
的非贪婪匹配,但它仍然是贪婪和过度匹配的:
var result = Regex.Match(text, "heading1\n(.*)\n{2}?", RegexOptions.Singleline)
.Groups[1]
.Value;
(我已经有了一个在"heading1"
到"heading2"
之间搜索的解决方案,但我更喜欢使用上面的方法作为非贪婪匹配的学习练习.)
UPDATE:
My question is simpler, has source text, is for C# specifically. It's easier to understand the problem. The linked question is technically relevant, but harder to apply to this case.