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

  • Home
  • SEARCH
  • 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 6719393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:05:16+00:00 2026-05-26T09:05:16+00:00

A friend gave me this piece of code and said there is a bug.

  • 0

A friend gave me this piece of code and said there is a bug. And yes, this code runs for ever.

The answer I got is:

It runs for >10^15 years before printing anything.

public class Match {
     public static void main(String[] args) {
         Pattern p = Pattern.compile("(aa|aab?)+");
         int count = 0;
         for(String s = ""; s.length() < 200; s += "a")
             if (p.matcher(s).matches())
                 count++;
         System.out.println(count);
     }
}

I didn’t really understand why am I seeing this behavior, I am new to java, do you have any suggestions?

  • 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-26T09:05:17+00:00Added an answer on May 26, 2026 at 9:05 am

    The pattern you are using is known as an evil regex according to OWASP (they know what they’re talking about most of the time):

    https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS

    It basically matches aa OR aa or aab (since the b is optional by addition of ?)

    A Regex like this is vulnerable to a ReDoS or Regex Denial of Service Attack.

    So yes, sort out what you want to match. I suggest in the above example you should simply match aa, no need for groups, repitition or alternation:

    Pattern p = Pattern.compile("aa");
    

    Also as someone pointed out, who now deleted his post, you should not use += to append to strings. You should use a StringBuffer instead:

    public class Match {
      public static void main(String[] args) {
        Pattern p = Pattern.compile("aa");
        StringBuffer buffy = new StringBuffer(200);
        int count = 0;
        for(int i = 0; i < 200; i++){
          buffy.append("a")
          if (p.matcher(buffy.toString()).matches()){
            count++;
          }
        }
        System.out.println(count);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A friend gave me this code snippet in Clojure (defn sum [coll acc] (if
Alright, my friend gave me this code for requesting headers and comparing them to
A friend gave me a solution that uses this jquery code: .css({ backgroundColor: '#ddd'
A friend show me this sample code to implement HTTP POST in C# and
a friend of mine gave me a piece of his software and I'm trying
A friend of mine gave me this task so I can learn advance programming
Before I get bombarded with RTMs, I did google this, but all I got
my friend gave me a program which runs in his laptop, he coded with
My friend gave me a bit of code for my SDL program, all I
My friend gave the following code which is working awesome here http://jsfiddle.net/WVLE2/ I tried

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.