You're fortunate because in fact .NET regex does this (which I think is quite unique). Essentially in every Match
, each Group
stores every Captures
that was made.
So you can count how many times a repeatable pattern matched an input by:
- Making it a capturing group
- Counting how many captures were made by that group in each match
- You can iterate through individual capture too if you want!
Here's an example:
Regex r = new Regex(@"(hu?a)+");
var text = "hahahaha that's funny but not huahuahua more like huahahahuaha";
foreach (Match m in r.Matches(text)) {
Console.WriteLine(m + " " + m.Groups[1].Captures.Count);
}
This prints (as seen on ideone.com):
hahahaha 4
huahuahua 3
huahahahuaha 5
API references
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…