Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:34:17+00:00 2026-05-30T08:34:17+00:00

I currently have a regex pattern that matches a specific word, which includes arbitrary

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:34:18+00:00Added an answer on May 30, 2026 at 8:34 am

    I believe I came up with a sensible solution:

    // Possible values for n:
    // (t\\s*h\\s*e\\s*){1}
    // (t\\s*h\\s*e\\s*){2}
    // (t\\s*h\\s*e\\s*){3}...
    public int consecutiveThe(String s) {
        int n = 0;
        while(true) {
             String expression = "(t\\s*h\\s*e\\s*){" + n + "}";
             Pattern p = Pattern.compile(expression);
             Matcher m = p.matcher(s);
             if(!m.find()) {
                break;
             }
             n++;
        }
        return (n - 1);
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to alter a RegEx pattern. I currently have this which matches all
Currently (in PHP) I have the following regex pattern: \[(.*) (.*)=(.*)\] This matches [doSomething
Currently I have a SQL server column of type nvarchar(max) which has text that
I have the following Regex (in PHP) that I am using. '/^[a-zA-Z0-9&\'\s]+$/' It currently
I have been trying to create a regular expressions pattern that matches any reference
I have to write a regular expression that will match the following patterns, it
I have a regex and replacement pattern that have both been tested in Notepad++
I have the following regex pattern which works fine: #^(.+?)=(.+?)$#D However I want to
I'm currently trying to wrap my head around regex, I have a validation snippet
I currently have an MS Access application that connects to a PostgreSQL database via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.