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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:45:12+00:00 2026-06-13T18:45:12+00:00

I need to validate a user given String and validate that it is a

  • 0

I need to validate a user given String and validate that it is a valid Set, possibly a set that contains inner sets. Examples:

 1) {1, 2, 3, 4} = valid
 2) {1, 2, {3, 4}, 5} = valid
 3) 1, 2, 3, 4 = invalid (missing brackets)
 4) {1, 2, {3, 4, 5} = invalid (missing inner bracket)

This is the regex I am using (broken up for readability):

String elementSeparator = "(,\\s)?";
String validElement = "(\\{?[A-Za-z0-9]*\\}?" + elementSeparator + ")*";
String regex = "^\\{" + validElement + "\\}$";

Currently the it accepts Sets with optional opening and closing brackets, but I need it to only accept if they are both there, and not if an inner set is missing a bracket. In my current implementation the 4th example is accepted as a valid set.

How can I accomplish this?

  • 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-13T18:45:14+00:00Added an answer on June 13, 2026 at 6:45 pm

    Here’s some Java pseudo-code for how to approach this problem without using any heavyweight tools such as ANTLR. The basic approach is to split the input into tokens consisting of

    1. A single open or close brace
    2. A comma
    3. Whitespace
    4. An identifier

    Then you scan through the tokens, keeping track of the nesting level as you go. If when you get to the end the nesting level isn’t zero, the input string has an unbalanced brace.

    Pattern token = Pattern.compile("([{}]|,|[A-Aa-z0-9]+|\s+)");
    int nesting = 0
    Matcher m = token.matcher(inputString);
    while(m.find())
    {
        if (m.group(1).equals("{")
            nesting++;
        else if (m.group(1).equals("}")
        {
            nesting--;
            if (nesting < 0)
                error - too many right braces
        }
        else
            ....
    }
    if (nesting != 0) 
        log("incorrect nesting");
    

    Once you have this framework in place you can enhance it to detect things like two commas in a row: set a flag when you see a comma, clear the flag when you see an identifier (but not whitespace). In the branch for comma and close brace you test the flag and issue an error message since a comma at that point is not valid. And so on, for whatever validation you need.

    Note that my pseudocode above is not a complete solution, just intended to give you the general approach. A complete solution would be somewhat more involved as it would have to deal with invalid characters, making the lexer (the part that breaks up the string into tokens) more complex.

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

Sidebar

Related Questions

I need to validate a user's screen name to make sure that it can
I need to validate a given Name field in the asp.net form. User Name:
I need to validate two user input fields against each other in seam. Field1
I need to validate if the user's provided information is in the database, i've
I need to validate input to my application. The input is a formatted string
I need to validate a basic authorization header that is being sent to my
I need a way to validate a user/password pair for native c++ on windows.
Given a username and password, I need to check if there's a user on
i need to validate this type of string 0415256987, 0452 654 987, 0411 236589,
I'm trying to validate that the input a user gives my program via gets

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.