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

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For me, There is no violation against DI in your… May 16, 2026 at 5:06 pm
  • Editorial Team
    Editorial Team added an answer the following XSLT 2.0 transformation will do what you request:… May 16, 2026 at 5:06 pm
  • Editorial Team
    Editorial Team added an answer You're looking for Windows 7 Thumbnail Toolbars: Thumbnail toolbars provide… May 16, 2026 at 5:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I would like like to create a java regular expression that selects everything from
I need to create a regular expression that allows a string to contain any
anyone have any idea about how to create java program that can read barcode
I need to identify (potentially nested) capture groups within regular expressions and create a
Related: How do I create a static local variable in Java? Pardon if this
Say I want to create a sort of Pre-processor for existing java code, so
The question is whether can I create an instance of class (which is situated
How can I have a text file (or XML file) represented as a whole
Hi I am trying to write a java applet that will get some hard
I am parsing documents which contain large amounts of formatted numbers, an example being:

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.