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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:10:44+00:00 2026-06-08T02:10:44+00:00

I want to get out numbers from a line with pattern, but it wont

  • 0

I want to get out numbers from a line with pattern, but it wont group numbers as I would like.

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("(.*?)((\\d+),{0,1}\\s*){7}");
    Scanner in = new Scanner("text: 1, 2, 3, 4, 5, 6, 7"); // new Scanner(new File("data.txt"));
    in.useDelimiter("\n");

    try {
        while(!(in.hasNext(pattern))) {
            //Skip corrupted data
            in.nextLine();
        }
    } catch(NoSuchElementException ex) {
    }
    String line = in.next();
    Matcher m = pattern.matcher(line);
    m.matches();
    int groupCount = m.groupCount();
    for(int i = 1; i <= groupCount; i++) {
        System.out.println("group(" + i + ") = " + m.group(i));
    }
}

Output:

group(1) = text:

group(2) = 7

group(3) = 7

What I want to get is:

group(2) = 1

group(3) = 2

…

group(8) = 7

Can I get this from this one pattern or should I make another one ?

  • 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-08T02:10:45+00:00Added an answer on June 8, 2026 at 2:10 am

    You cannot. Groups always correspond to capturing groups in the regular expression. That is, if you have one capturing group, there cannot be more than one group in the match. It is irrelevant how often a portion (even a capturing group) is repeated during the match. The expression itself defines how many groups the final match can have.

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

Sidebar

Related Questions

Why would I want to get out of an Error Handler (after handling) with
I want to get print out invoice but also wanna set margins and page
I want to get some useful information from NSError . If I print out
I want to get the out parameter of stored procedure using Zend framework Here's
Given a table row, I want to get the HTML out of the span
I want to get the text field test out of this web page. How
I want to get all the error messages out of the modelState without knowing
What I want to do is to get Json data out as a list
I want to do the following,but I get errors: procedure JumpToCodeCave(CurrentLocation:DWORD;Destination:Pointer;out_JmpBack:Pointer); var calc:DWORD; jmppatch:Array[0..3]
I'm trying to figure out if I can get what I want out of

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.