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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:39:28+00:00 2026-05-17T00:39:28+00:00

I need some help writing a regular expression that matches this: an opening square

  • 0

I need some help writing a regular expression that matches this:

  • an opening square bracket OR number as first character
  • a closing square bracket (if there was an opening square bracket) OR number (if the opening was a number) as the last character
  • at least one comma separating two numbers without a space, and if there are more commas, they must be seperating numbers without spaces

Example matches:

  • 1,1
  • [1,1]
  • [1,1,2,3]

Not matches:

  • [1,1
  • 1, 1
  • [1, 2,2]
  • [1,2,]
  • 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-17T00:39:28+00:00Added an answer on May 17, 2026 at 12:39 am

    Assuming that “numbers” are one or more occurrences of the digit class \d, one regular expression would be:

    ^\d+(,\d+)+$|^\[\d+(,\d+)+\]$
    

    Other variants of “numbers” might be [1-9][0-9]* which would not allow leading zeroes and would be closer to the C/C++/C#/Java representation of a decimal integer literal. Leading zeroes in C/C++/C#/Java, for example, designate octal integer literals. In Java, I believe that 0 is officially a decimal literal so a more accurate expression would be 0|[1-9][0-9]* while in C++ 0 is technically an octal literal (I think!).

    Assuming Java-style integer literals, our expression would then become:

    ^(0|[1-9][0-9]*)(,(0|[1-9][0-9]*)+)+$|^\[(0|[1-9][0-9]*)+(,(0|[1-9][0-9]*)+)+\]$
    

    Whatever you do, you need to decide what constitutes “number” in your situation which may or may not be the same as the programming language you are implementing your program in.

    Useful links:

    • Java lexical specification: http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html – section 3.10.1 describes integer literals
    • Representation of C++ integer literals: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/conref.htm
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.