tags:

views:

37

answers:

1

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  A: 

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:

Richard Cook
I think your regular expresion matches this invalid case: http://www.rubular.com/r/uV6esNPA50
regExNewb
I just added more anchor characters (`^` and `$`) to fix this. Sorry about that.
Richard Cook