.NET Regex number of matched matches

I have this RegEx which finds any permutation with one A, one B and two C

 (?:(?<A>A)|(?<B>B)|(?<C>C)){4}(?<-A>)(?<-B>)(?<-C>){2}

      

for example, for this combination we have 3 matches (positions 1, 7, 15)

 ABCCABCABCABCAABCC

      

We need to know how many overlapping matches we have. In this case, when we find a match in the first 4 positions, it starts looking for another match in position 5.

We need it to start looking for the next match at position 2, so matches will be at positions: 1, 2, 3, 4, 7, 10, 15

In this example, we will have 7 matches

 1. ABCC
 2. BCCA
 3. CCAB
 4. CABC
 7. CABC
 10. CABC
 15. ABCC

      

How can I use RegEx to start searching for the next match at the next position instead of the next position after the complete sequence?

Thanks in advance.

+3


source to share


2 answers


You need to use a capture group inside to look ahead :

See here :

Lookahead assertions do not consume any characters in the string. As a result, you can use them to find overlapping sequences of characters.

(?=(?<value>(?:(?<A>A)|(?<B>B)|(?<C>C)){4}(?<-A>)(?<-B>)(?<-C>){2}))
   ^                                                               ^

      



If you want to save a match of the regular expression within lookahead, you need to put parentheses around the staple of the regular expression within lookahead, for example (?=(regex))

.

More information about matching regex matches can be found at Rexegg.com.

See demo

enter image description here

+2


source


If you want a general way to match matches for any regex, just follow the loop logic:

var re = new Regex(@"(?:(?<A>A)|(?<B>B)|(?<C>C)){4}(?<-A>)(?<-B>)(?<-C>){2}");
var input = "ABCCABCABCABCAABCC";

var m = re.Match(input);
while (m.Success)
{
    Console.WriteLine(m.Value);

    // End of string reached
    if (m.Index == input.Length)
        break;

    // Match from the next index
    m = re.Match(input, m.Index + 1);
}

      



You can even extract this into an extension method:

public static IEnumerable<Match> OverlappingMatches(this Regex regex, string input)
{
    var match = regex.Match(input);

    while (match.Success)
    {
        yield return match;

        if (match.Index == input.Length)
            break;

        match = regex.Match(input, match.Index + 1);
    }
}

      

+2


source







All Articles