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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:16:00+00:00 2026-06-17T14:16:00+00:00

I have the following regex pattern: ^(\d+)(;(\d+))*$ . And I would like to get

  • 0

I have the following regex pattern: ^(\d+)(;(\d+))*$. And I would like to get the number of groups in that regex and the value of each.

I tried using groupCount and group but I get the following results:

Input: "1"
Groups: 3
"1", "1", null, null 
Input: "1;2"
Groups: 3
"1;2", "1", ";2", "2"
Input: "1;2;3"
Groups: 3
"1;2;3", "1", ";3", "3"
Input: "1;2;3;4"
Groups: 3
"1;2;3;4", "1", ";4", "4"

I was expecting for the first one "1" to get 1 from groupCount. And in the case of the last, "1;2;3;4", I was expecting to get 7 from groupCount.

Is there any method on Matcher that returns what I was expecting?

EDIT: Added the code that generated the above output

String input = "1";
Pattern pattern = Pattern.compile("^(\\d+)(;(\\d+))*$");
for (int i = 2; i < 6; ++i) {
    Matcher matcher = pattern.matcher(input);
    matcher.matches();
    System.out.println("Input: \"" + input + "\"\nGroups: " + matcher.groupCount());
    for (int group = 0; group <= matcher.groupCount(); ++group) {
        System.out.print("\"" + matcher.group(group) + "\", ");
    }
    System.out.println();
    input += ";" + i;
}
  • 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-17T14:16:02+00:00Added an answer on June 17, 2026 at 2:16 pm

    I am sorry, but there is a misunderstanding on your side about groups.

    You define the number of groups with your regex. It is not dependent on the string. And in your regex you define 3 groups:

     ^(\\d+)(;(\\d+))*$
      1     2 3
    

    The Groups are numbered by the opening brackets. So you regex will always have exactly 3 groups. If they match something is something completely different.

    So, in the first group there will always be the first found number. For the other two groups, you are doing something special: You are repeating a capturing group.

    Since the following numbers you are matching are all stored in group 3, you will find only the last one in the final result. In .net you would be able to read out all matches, but I don’t think it is possible in Java.

    Solution:

    Validate the String with a regex

    ^\\d+(;\\d+)*$
    

    And if the format is OK, then get the numbers by doing a split on “;”

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

Sidebar

Related Questions

I have the following REGEX that checks for a pattern like (alpha and decimals
I have the following string that would require me to parse it via Regex
I have the following regex I would like to escape in Objective-C /\B\$((?:[0-9]+(?=[a-z])|(?![0-9\.\:\_\-]))(?:[a-z0-9]|[\_\.\-\:](?![\.\_\.\-\:]))*[a-z0-9]+)/ig; Not
I have the following string, which matches the following RegEx string. I would like
I would like to have a RegEx that matches several of the same character
I have the following regex which validates dates: //are not both dates if (!methods._isDate(first[0].value)
Given the following text: This is!!xa simple string!xpattern I would like to get a
I have a file with the following grammar: <whitespace_sequence><string><whitespace_sequence><--More--><whitespace_sequence><string_sequence><newline> Using Python (2.4), I would
Lets say I have a multiline textbox that I would like to have checked
What would be a regex pattern for the following ' and ® in Java.

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.