I currently have a regex pattern that matches a specific word, which includes arbitrary whitespace.
e.g. if the word was “the”, my pattern will match “t h e” as well as ” the”
My question is, is there any way to count and track the number of consecutive repeats?
I am looking to return the largest amount of consecutive repeats of the word.
e.g. if my string was “the quick brown fox thethe jumped thethethe over the…”
I would want my method to return 3, not 7. Counting the total number of occurrences is very straightforward:
Pattern p = Pattern.compile("(t\\s*h\\s*e\\s*)");
Matcher m = p.matcher(s);
while(m.find()) {
count++;
}
I would like to return the greatest number of consecutive repetitions.
Just curious if there is a way to do this with regex.
I believe I came up with a sensible solution:
The idea is to loop through consecutive values of n, checking if there is a regex match. As soon as we have a value of n that is unmatched, return the number that was most previously matched in the sequence.