How can I define repetitive groups in Java Regex?
Lets say a 2digit number [0-9]{2} multiple times separates by ,
12,34,98,11
Is that p
The simplest way is to use a two-step solution: 1) first, validate the string, then 2) split the string with the delimiter of your choice:
String[] chunks = null;
if (s.matches("\\d{2}(?:,\\d{2})*")) {
chunks = s.split(Pattern.quote(","));
System.out.println(Arrays.toString(chunks)); // => [12, 34, 98, 11]
}
Here, s.matches("\\d{2}(?:,\\d{2})*")
matches the whole string that starts with two digits and then contains 0 or more occurrences of ,
and two digits to the end, and then s.split(Pattern.quote(","))
splits the string with a comma. Note you do not need ^
and $
anchors with a pattern inside matches()
since the method requires a full string match.
If you have to do that with a single regex, you may use multiple matches anchored to the start of string and end of each successful preceding match, only if the string check at the start of the string is a success:
(?:\G(?!^),|^(?=\d{2}(?:,\d{2})*$))(\d{2})
See the regex demo.
Details
(?:\G(?!^),|^(?=\d{2}(?:,\d{2})*$))
- end of the preceding successful match and then a ,
(see \G(?!^),
) or (|
) start of string (^
) that is followed with two digits and then 0 or more sequences of a ,
and two digits to the end of the string (see \d{2}(?:,\d{2})*$
)(\d{2})
- Group 1: two digitsJava demo:
String s = "12,34,98,11";
Pattern p = Pattern.compile("(?:\\G(?!^),|^(?=\\d{2}(?:,\\d{2})*$))(\\d{2})");
Matcher m = p.matcher(s);
List results = new ArrayList<>();
while(m.find()) {
results.add(m.group(1));
}
System.out.println(results);
// => [12, 34, 98, 11]