Search code examples
javaregexregex-group

Java matcher group capturing


EDIT: I'd also need, if possible, to have the regex match the string format

[id='value1' or id='value2' or ... or id='valueN'] 

and also capture all the values.

I have a piece of code that doesn't work as I'd expect and I'm not sure where's my mistake. Basically I have a string like

[id='id1' or id='id2' or id='id3'] 

and I need to find all the values such as id1, id2, and id3.

final String regex = "\\[id='([^']+)'(?:\\s*or\\s*id='([^']+)')*\\]";
final String text = "[id='id1' or id='id2' or id='id3']";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(text);

If I do

while(matcher.find())
{
   System.out.println(matcher.group(1));
}

I only get

id1

With

if (matcher.matches())
{
   System.out.println("Groups count: " + matcher.groupCount());
   for (int i = 1; i <= matcher.groupCount(); i++)
   {
       System.out.println(matcher.group(i));
   }
}

I get

id1
id3

Expected output would be

id1
id2
id3

Neither output is the expected one. Could someone please point me if the regex is wrong or is it because of the group finding part of the code?


Solution

  • You could use a regex to match all the id values.

    String s = "[id='id1' or id='id2' or id='id3']";
    Pattern regex = Pattern.compile("(?<=id=')[^']+");
    Matcher matcher = regex.matcher(s);
    while(matcher.find()){
            System.out.println(matcher.group(0));
    }
    

    Output:

    id1
    id2
    id3