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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:45:28+00:00 2026-06-06T17:45:28+00:00

I am testing a small stub of Matcher and Pattern class…see the following small

  • 0

I am testing a small stub of Matcher and Pattern class…see the following small stub..

package scjp2.escape.sequence.examples;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Sample_19 {

    public static void main(String a[]){
        String stream = "ab34ef";
        Pattern pattern = Pattern.compile("\\d*");

        //HERE * IS GREEDY QUANTIFIER THAT LOOKS FOR ZERO TO MANY COMBINATION THAT 
        //START WITH NUMBER 
        Matcher matcher = pattern.matcher(stream);

        while(matcher.find()){
            System.out.print(matcher.start()+matcher.group());
        }
    }

}

Here …our string which we are comparing is “ab34ef”. which is of length 6.

Noe let see the iteration…


Iteration NO matcher.start() matcher.group()

1 0 “”

2 1 “”

3 2 34

4 4 “”

5 5 “”

Now ..let combine…matcher.start() + matcher.group()….
the output as per our calculation is : 0123445

But, the stub generates 01234456.

I am not able to understand from where the “6” is coming.
String index starts from zero and so here there can be maximum index is 5.So from where 6 is coming??

It iterates over the loop six times..How ?
Any suggestion ?

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

    Your regular expression can match zero characters. The final match is a zero width string occurring at the end of the string, after the character at index 5. The index of this zero width string is therefore 6.


    As an aside, you might also find it easier to understand what is going on if you use separators to make the output more readable:

    System.out.println(matcher.start()+ ": " + matcher.group());
    

    Results:

    0: 
    1: 
    2: 34
    4: 
    5: 
    6: 
    

    ideone

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

Sidebar

Related Questions

Recieved the following error whilst testing a small apache strut based web application ive
I have small code as shown below public class Testing { public static void
I am using jstl for small testing. And it is not working how it
I'm trying to build a small testing app with erlang+mnesia. I have a user
Here a small piece of code for testing and explain the problem. I have
I'm testing an app that reads thousands of small objects and sends then back
I'm writing a small web server for testing purposes using python, BasicHTTPServer and SimpleHTTPServer.
I'm having an small issue with SQLXML in SQL Server 2008. Here's my testing
I'm writing a small free tool. It's currently in Beta testing using .NET 3.5
testing Play framework and JPA. I eclipsify my very simple test application. Import the

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.