I wonder if it is possible to get MatchCollection with all matches even if there's intersection among them.
string input = "a a a";
Regex regex = new Regex("a a");
MatchCollection matches = regex.Matches(input);
Console.WriteLine(matches.Count);
This code returns 1, but I want it to return 2. How to achive it?
Thank you for your help.
Intersection is not a part of standard regular expressions.
The intersection of several regexes is one regex that matches strings that each of the component regexes also match. The General Option. To check for the intersection of two patterns, the general method is (pseudo-code): if match(regex1) && match(regex2) { champagne for everyone! }
string input = "a a a";
Regex regexObj = new Regex("a a");
Match matchObj = regexObj.Match(input);
while (matchObj.Success) {
matchObj = regexObj.Match(input, matchObj.Index + 1);
}
will iterate over the string starting the next iteration one character after the position of the previous match, therefore finding all matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With