3

I'm using Java's regex library. I want to validate a string against the following format:

31,5,46,7,86(...)

The amount of numbers is not known. I want to make sure there is at least one number in that string, and that every two numbers are separated by a comma. I also want to get the numbers from the string.

(Note: this is just a simplified example, string.split will not solve my actual problem)

I wrote the following regex:

({[0-9]++)((?:,[0-9]++)*+)

The validation part works. However, when I try to extract the numbers, I get 2 groups:

Group1: 31
Group2: ,5,46,7,86

regex101 version: https://regex101.com/r/xJ5oQ6/3

Is there a way I can get each number separately? i.e. to end up with the collection:

[31, 5, 46, 7, 86]

Thanks in advance.

Roy Millo
  • 33
  • 1
  • 4

2 Answers2

5

Java does not allow you to access the individual matches of a repeated capturing group. For more information look at this question: Regular Expression - Capturing all repeating groups

The code provided by Tim Pietzcker can help you as well. If you rework it a bit and add a special case for the first number you can use something like this:

String target = "31,5,46,7,86";

Pattern compileFirst = Pattern.compile("(?<number>[0-9]+)(,([0-9])+)*");
Pattern compileFollowing = Pattern.compile(",(?<number>[0-9]+)");

Matcher matcherFirst = compileFirst.matcher(target);
Matcher matcherFollowing = compileFollowing.matcher(target);

System.out.println("matches: " + matcherFirst.matches());
System.out.println("first: " + matcherFirst.group("number"));

int start = 0;
while (matcherFollowing.find(start)) {
    String group = matcherFollowing.group("number");

    System.out.println("following: " + start + " - " + group);
    start = matcherFollowing.end();
}

This outputs:

matches: true
first: 31
following: 0 - 5
following: 4 - 46
following: 7 - 7
following: 9 - 86
Community
  • 1
  • 1
Leon
  • 2,926
  • 1
  • 25
  • 34
0

This might work for you:

/(?=[0-9,]+$)((?<=,|^)[0-9]{1,2})(?=,|$)/g

The above captures one or two digits followed by , or end of input.

Note that I used the global modifier.

Try it online

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • Now the captures are working, but the validation isn't: I tried to add a letter in the middle and instead of 'no match' [i got this](https://regex101.com/r/xJ5oQ6/5). Your suggestion is the closest I've got to what I wanted though, any ideas how to tweak it? – Roy Millo Jun 10 '16 at 14:42
  • @RoyMillo I updated the regex, but what you want is not possible to put in one PCRE regex, since dynamic length lookbehinds are not supported. I don't know what regex flavor Java is using. – Andreas Louv Jun 10 '16 at 14:48
  • Your validation regex could however look something like this: `/^[0-9,]+$/` – Andreas Louv Jun 10 '16 at 14:49