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 9180039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:56:57+00:00 2026-06-17T17:56:57+00:00

In order to re-produce the problem as stated in a recent question – Why

  • 0

In order to re-produce the problem as stated in a recent question – Why does (.*)* make two matches and select nothing in group $1? I tried various combination of * and +, inside and outside the brackets, and the result I got was not expected.

I would have expected the output, same as one explained in the accepted answer in that question, and also in another duplicate question, tagged under Perl – Why doesn't the .* consume the entire string in this Perl regex? . But it’s not behaving the same way.

To make it simple, here’s the code I tried: –

String str = "input";
String[] patterns = { "(.*)*", "(.*)+", "(.+)*", "(.+)+" };

for (String pattern: patterns) {
    Matcher matcher = Pattern.compile(pattern).matcher(str);

    while (matcher.find()) {
        System.out.print("'" + matcher.group(1) + "' : '" + matcher.start() + "'" + "\t");
    }

    System.out.println();
}

And this is the output I got for all the 4 combination: –

'' : '0'    '' : '5'            // For `(.*)*`
'' : '0'    '' : '5'            // For `(.*)+`  
'input' : '0'   'null' : '5'    // For `(.+)*`
'input' : '0'                   // For `(.+)+`

Now, What I can’t understand, why in 1st and 2nd output, I am not getting the entire string as first result for matcher.find(). I mean, ideally, in 1st case, .* should first capture the entire string, and then also capture the empty string at the end. Now, although it is giving expected result for 2nd match, it’s not behaving well for 1st match.

And also, in 2nd case, I should not even get the 2nd match, because I’m having a + quantifier outside the bracket.

My expected output is: –

'input' : '0'   '' : '5'  // For 1st
'input' : '0'    // For 2nd

Also, in the 3rd output, why I got null as 2nd match instead of empty string? Shouldn’t the 2nd match for first 3 combination be same?

4th output is as per expectation. So, no doubt in that.

  • 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-06-17T17:56:59+00:00Added an answer on June 17, 2026 at 5:56 pm

    You’re seeing the effect of the same phenomenon you see in the question you linked to:

    For (.*)*:

    • The first matcher.start() is 0 because that’s where the match ("input") starts.
    • The first matcher.group(1) is "" because the repeated (.*) has overwritten the captured "input" with the empty string (but matcher.group(0) does contain input").
    • The second matcher.start() is 5 because that’s where the regex engine is after the first successful match.
    • The second matcher.group(1) (as well as matcher.group(0)) is "" because that’s all there was to match at the end of the string.

    For (.*)+ it’s the same. After all, the empty string can be repeated as many times as you want and still be the empty string.

    For (.+)* you get null because while the second match succeeds (zero repetitions of a string of length 1 matches the empty string), the capturing parentheses haven’t been able to capture anything, so its contents are null (as in undefined, instead of the empty string).

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

Sidebar

Related Questions

I've got a little problem with an order by in mysql request. Here, is
If a function has 2 arguments and both produce exceptions, what order would you
I have a problem with my code: <?php echo $s_country; $sql2=SELECT prod_id FROM tbl_order_item
The problem comes from real environment where the production_plan table captures the order identification
I'm reading lines of text that can come in any order. The problem is
I have the following table: Order Product Price Quantity Description Order1 Product1 12 1
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
I have a basic Customer/Order/OrderItem/Product object graph. Customer has Many Orders, Order has Many
Here are the relevant models: User Product Order A User can sell or buy
I'm building an e-commerce website. I have a Product and Order models. It's possible

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.