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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:51:08+00:00 2026-05-27T12:51:08+00:00

I am really struggling with this question: import java.util.regex.*; class Regex2 { public static

  • 0

I am really struggling with this question:

import java.util.regex.*;    
class Regex2 {    
    public static void main(String[] args) {    
        Pattern p = Pattern.compile(args[0]);    
        Matcher m = p.matcher(args[1]);    
        boolean b = false;    
        while(b = m.find()) {    
            System.out.print(m.start() + m.group());    
        }    
    }
}  

When the above program is run with the following command:

java Regex2 "\d*" ab34ef 

It outputs 01234456. I don’t really understand this output. Consider the following indexes for each of the characters:

a b 3 4 e f
^ ^ ^ ^ ^ ^
0 1 2 3 4 5

Shouldn’t the output have been 0123445?

I have been reading around and it looks like the RegEx engine will also read the end of the string but I just don’t understand. Would appreciate if someone can provide a step by step guide as to how it is getting that result. i.e. how it is finding each of the numbers.

  • 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-27T12:51:09+00:00Added an answer on May 27, 2026 at 12:51 pm

    It is helpful to change

    System.out.print(m.start() + m.group());
    

    to

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

    This way the output is much clearer:

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

    You can see that it matched at 7 different positions: at position 2 it matched string “34” and at any other position it matched an empty string. Empty string matches at the end as well, which is why you see “6” at the end of your output.

    Note that if you run your program like this:

    java Regex2 "\d+" ab34ef
    

    it will only output

    2: 34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://jsfiddle.net/motocomdigital/7fyvn/1/ Hello, this is a question I'm really struggling to find an answer for.
OK i am really struggling with this! I know had to add an image
For some reason I'm really struggling with this. I'm new to wpf and I
Hey, I'm really struggling with this one. I'am trying to port a small piece
I am really struggling with this issue as it seems to occur randomly for
I'm struggling since a few hours with this design question. In Cocoa for example,
This is probably a really easy question to answer, but for some reason I'm
I'm really struggling with this query. I have 4 tables (http://oberto.co.nz/db-sql.png): Invoice_Payement, Invoice, Client
I am really struggling with this. I have decided to switch to Passenger after
I don't really know where to begin asking this question, so I'll just try

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.