I have issues to get all capture groups from a regular expression and I believe I am doing it wrong, but I quite don’t know what the correct approach would be.
Given regular expression: ^(([A-Za-z0-9]+)(?::((?:[a-z]{2})|\*))?\s*(=)\s*)?("?)([^"\s]+)\5$
Given value to match: property:xy = "value"
In Java I would do something like that:
import java.util.regex.Pattern;
class Scratch {
public static void main(String[] args) {
var regex = "^(([A-Za-z0-9]+)(?::((?:[a-z]{2})|\\*))?\\s*(=)\\s*)?(\"?)([^\"\\s]+)\\5$";
var string = "property:xy = \"value\"";
var matcher = Pattern.compile(regex).matcher(string);
if (matcher.matches()) {
System.out.println(">>> Count: " + matcher.groupCount());
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(6));
}
}
}
Now, over here in Scala I tried that:
val regex = "^(([A-Za-z0-9]+)(?::((?:[a-z]{2})|\\*))?\\s*(=)\\s*)?(\"?)([^\"\\s]+)\\5$".r
val string = "property:xy = \"value\""
val result = regex.findAllIn(query)
The size of the result is 1, but I would expect 6.
How can I accomplish in Scala what I did in Java?