0

I'm trying to implement the regex described on the title...here's what i have so far:

"(^\\d{1,2})+(,\\d{1,2})*$"

It works when my input is like this:

1,2,3,4,5,6,7 and so goes on

But i'd like to allow the user to add white spaces as he/she wants:

1 ,     2,3,4    ,5

I know it's a bit weird but it's a flexibility i'd like to give to the final user. How can i acomplish this?

EDIT: I forgot to mention that the spaces are only allowed before or after the one or two digits;
EDIT2: I also forgot to mention that after the comma, can have white spaces too.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Joao Victor
  • 1,111
  • 4
  • 19
  • 39
  • possible duplicate of [Regex allowing a space character in Java](http://stackoverflow.com/questions/5601754/regex-allowing-a-space-character-in-java) – Smutje Jul 02 '14 at 12:48
  • I am not sure what you want to achieve. Do you want to check if string contains only digits separated with `,` optionally surrounded with some spaces? Or you want to actually find numbers if they have comma (and maybe few spaces) after them? Could you post example of output you want to get? – Pshemo Jul 02 '14 at 13:29
  • I want to achieve this: 1, 3,4,5 ,6 or this: 1 , 3,4, 5, 10, 23 (there could be as many white spaces before and after the digits) – Joao Victor Jul 02 '14 at 13:32
  • It looks like input. I was asking about expected output (based on input), preferably with logic explaining why it is expected. – Pshemo Jul 02 '14 at 13:33
  • Actually i just want to validate the input...it will be used in a IN sql statement...as far as i know, you can have something like this: "WHERE ID IN ( 2,3,4 5, 10 )" – Joao Victor Jul 02 '14 at 13:35

6 Answers6

0

I would add optional whitespace in a positive lookahead, as such:

String input = "1, 2 , 3, 45 , 67,89";
//                           | 1 or 2 digits
//                           |       | start positive lookahead
//                           |       |  | optional whitespace
//                           |       |  |   | comma
//                           |       |  |   || or
//                           |       |  |   ||| end of input
Pattern p = Pattern.compile("\\d{1,2}(?=\\s*,|$)");
Matcher m = p.matcher(input);
while (m.find()) {
   System.out.println(m.group());
}

Output

1
2 
3
45 
67
89
Mena
  • 47,782
  • 11
  • 87
  • 106
0

Replace your ', ' with [, ]+

eg

[0-9]+([ ,]+[0-9]+)*
Chris K
  • 11,622
  • 1
  • 36
  • 49
0

use : \\d{1,2}(?=\\s*,*) .This will match your String

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Here's a verbose way that I think would work:

"(\\d{1,2}\\s*,\\s*)*"

user3758171
  • 171
  • 1
  • 6
0

As you know

^(\\d{1,2})+(,\\d{1,2})*$ 

works for

1,2,3,4,5,6,7

If you want to let , be surrounded by zero or more spaces you can write it as \\s*,\\s*. So to let regex match something like

1 ,     2,3,4    ,5

all you need to do is change

^(\\d{1,2})+(,\\d{1,2})*$ 

to

^(\\d{1,2})+(\\s*,\\s*\\d{1,2})*$ 
//           ^^^^^^^^^ - part changed

If you would also like to add spaces before and after your entire input also surround your regex with \\s* like

^\\s*(\\d{1,2})+(\\s*,\\s*\\d{1,2})*\\s*$ 
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Firstly, I would change

"(^\\d{1,2})+(,\\d{1,2})*$"

to

"^\\d{1,2}(,\\d{1,2})*$"

as that first bracket group won't be repeated, and then throw in a bunch of spaces:

"^\\s*\\d{1,2}\\s*(,\\s*\\d{1,2}\\s*)*$"
Zantier
  • 833
  • 1
  • 8
  • 18