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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:42:36+00:00 2026-05-29T11:42:36+00:00

Java Code: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[]

  • 0

Java Code:

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

public class RegExpTest {
    public static void main(String[] args) {

        String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001";
        String p = "Value = (.*?), ";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()){
             System.out.println(matcher.group(1));
             System.out.println(matcher.group(2));
             System.out.println(matcher.group(3));
        }
    }
}

Java code’s output:

$ java RegExpTest 
-0.525108
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 2
        at java.util.regex.Matcher.group(Matcher.java:487)
        at RegExpTest.main(RegExpTest.java:15)
$ 

Python code (in Interpreter):

>>> import re
>>> re.findall("Value = (.*?), ", 'X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001;')
['-0.525108', '7.746691', '5.863008']
>>> 

So, why is Java unable to match all the occurrences of the match?

  • 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-29T11:42:36+00:00Added an answer on May 29, 2026 at 11:42 am

    It’s because a group for a java match is a capturing bracket.

    Your regex only has one set of non-escaped (ie capturing) brackets, the (.*?).

    Group 1 contains the value that gets matched.

    There is no group 2 because there is no second set of brackets in your regex.

    In the java example, you want to loop through all matches, and print matcher.group(1).

    while ( matcher.find() ) {
        System.out.println(matcher.group(1));
    }
    

    Note the while, which will loop through all matches and tell you group 1 from each.

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

Sidebar

Related Questions

Here is my code: import java.util.*; public class Multiply { public static void main(String[]
import java.util.regex.*; import java.io.*; class Patmatch{ static String str = ; public static void
Consider this code: import java.util.regex.*; public class Pattern3 { /** * @param args the
I've got the following code: import java.util.*; public class Group { public static void
I have the below groovy code import java.util.regex.Matcher; import java.util.regex.Pattern; String myInput=License_All (12313) String
The following code matches the two expressions and prints success. import java.util.regex.Matcher; import java.util.regex.Pattern;
This is our code: import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {

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.