From a string, I need to pull out groups that match a given pattern.
An example string: <XmlLrvs>FIRST</XmlLrvs><XmlLrvs>SECOND</XmlLrvs><XmlLrvs>Third</XmlLrvs>
Each group shall begin with <XmlLrvs>
and end with </XmlLrvs>
. Here is a snippet of my code...
String patternStr = "(<XmlLrvs>.+?</XmlLrvs>)+";
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(text);
matcher.matches();
// Get all groups for this match
for (int i = 1; i<=matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
The output is <XmlLrvs>Third</XmlLrvs>
. I am expecting group first and second but those aren't being captured. Can anyone assist?