I essentially have a String representation of a comma separated list. However, each individual element is also comma separated, so the String was modified to have each element surrounded by '<' and '>'. I am trying to use regex to capture each element and add it to a list, thus making it a List of elements, rather than a String of a List.
Here are some example String inputs:
"<>" // should match regex, but will be thrown out
"<a=1>"
"<a=1,b=1>"
"<a=1,b=1>,<a=2,b=2>"
"<a=1,b=1>,<a=2,b=2>,<a=3,b=3,c=3>,<a=4>"
The corresponding outputs I would like would be lists like so:
["a=1"]
["a=1,b=1"]
["a=1,b=1","a=2,b=2"]
["a=1,b=1","a=2,b=2","a=3,b=3,c=3","a=4"]
The pattern I am trying to use is:
Pattern pattern = Pattern.compile("<([^>]*)>(,<([^>]*)>)*");
But when I try to create the list, it is not handling each additional occurrence as a new group.
Matcher matcher = pattern.matcher(myString);
if (matcher.matches()) {
List<String> listOfElements = new ArrayList<>();
for (int i = 1; i <= matcher.groupCount(); i++) { // group 0 represents the entire String, so start at index 1
if (matcher.group(i) != null) {
listOfElements.add(matcher.group(i));
}
}
System.out.println(listOfElements);
}
The result of the above test cases are:
["a=1"]
["a=1,b=1"]
["a=1,b=1", ",<a=2,b=2>", "a=2,b=2"]
["a=1,b=1", ",<a=4>", "a=4"]
Note: I added the quotes to that result for readability to separate out the values in the list - obviously the System.out.println() does not write out the quotes.
What is the proper regex to do this? Or if there is a better way than using regex, I'd be happy to hear, though keep in mind that I'd prefer to not have to use a third party package.
<and the final>, then split the rest on>,<? That'll give you what you want in an array, and you can then useArrays.asList.