I’m trying to get count the instances of 3 consecutive “a” events, "aaa".
The string will comprise the lower alphabet, e.g. "abaaaababaaa"
I tried the following piece of code. But the behavior is not precisely what I am looking for.
x<-"abaaaababaaa";
gregexpr("aaa",x);
I would like the match to return 3 instances of the “aaa” occurrence as opposed to 2.
Assume indexation begins with 1
- The first occurrence of “aaa” is at index 3.
- The second occurrence of “aaa” is at index 4. (this is not caught by
gregexpr) - The third occurrence of “aaa” is at index 10.
To catch the overlapping matches, you can use a lookahead like this:
However, your matches are now just a single “a”, so it might complicate further processing of these matches, especially if you’re not always looking for fixed-length patterns.