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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:21:31+00:00 2026-05-15T21:21:31+00:00

How can I create a java regular expression for a comma separator list (3)

  • 0

How can I create a java regular expression for a comma separator list

(3)
(3,6)
(3 , 6 )

I tried, but it does not match anything:

Pattern.compile("\\(\\S[,]+\\)")

and how can I get the value “3” or “3”and “6” in my code from the Matcher class?

  • 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-15T21:21:32+00:00Added an answer on May 15, 2026 at 9:21 pm

    Validation regex

    You can try this meta-regex approach for clarity:

        String pattern = 
            "< part (?: , part )* >"
                .replace("<", "\\(")
                .replace(">", "\\)")
                .replace(" ", "\\s*")
                .replace("part", "[^\\s*(,)]++");
    
        System.out.println(pattern);
        /*** this is the pattern
        \(\s*[^\s*(,)]+\s*(?:\s*,\s*[^\s*(,)]+\s*)*\s*\)
        ****/
    

    The part pattern is [^\s(,)]+, i.e. one or more of anything but whitespace, brackets and comma. This construct is called the negated character class. [aeiou] matches any of the 5 vowel letters; [^aeiou] matches everything but (which includes consonants but also numbers, symbols, whitespaces).

    The + repetition is also made possessive to ++ for optimization. The (?:...) construct is a non-capturing group, also for optimization.

    References

    • regular-expressions.info/Character Class, Possessive Quantifier, Non-capturing Group
    • java.util.regex.Pattern

    Testing and splitting

    We can then test the pattern as follows:

        String[] tests = {
            "(1,3,6)",
            "(x,y!,a+b=c)",
            "( 1,    3  , 6)",
            "(1,3,6,)",
            "(())",
            "(,)",
            "()",
            "(oh, my, god)",
            "(oh,,my,,god)",
            "([],<>)",
            "(  !!  ,  ??  ,  ++  )",
        };
    
        for (String test : tests) {
            if (test.matches(pattern)) {
                String[] parts = test
                    .replaceAll("^\\(\\s*|\\s*\\)$", "")
                    .split("\\s*,\\s*");
    
                System.out.printf("%s = %s%n",
                    test,
                    java.util.Arrays.toString(parts)
                );
            } else {
                System.out.println(test + " no match");
            }
        }
    

    This prints:

    (1,3,6) = [1, 3, 6]
    (x,y!,a+b=c) = [x, y!, a+b=c]
    ( 1,    3  , 6) = [1, 3, 6]
    (1,3,6,) no match
    (()) no match
    (,) no match
    () no match
    (oh, my, god) = [oh, my, god]
    (oh,,my,,god) no match
    ([],<>) = [[], <>]
    (  !!  ,  ??  ,  ++  ) = [!!, ??, ++]
    

    This uses String.split to get a String[] of all the parts after trimming the brackets out.

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

Sidebar

Related Questions

Does exists a java library that can create sql statements? I'm not in search
I can create an empty Java enum type, but when i want to add
I would like like to create a java regular expression that selects everything from
In Java can I create a URI for a file located locally in the
How to create a java plugin that can directly access the microphone for specific
How can I create a barcode image in Java? I need something that will
Am trying to create a java project which can able to send push notification
Can I fire show create table query in Java code for mysql? What I
We can't use java.text.SimpleDateFormat, so is there any way to create date object from
I need create a document word with Java. And I ask, how can I

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.