Search code examples
javaregexstringregex-group

Java Regex inconsistent groups


Please Refer to following question on SO:

Java: Regex not matching

My Regex groups are not consistent. My code looks like:

public class RegexTest {

    public static void main(String[] args) {

        // final String VALUES_REGEX = "^\\{([0-9a-zA-Z\\-\\_\\.]+)(?:,\\s*([0-9a-zA-Z\\-\\_\\.]*))*\\}$";
        final String VALUES_REGEX = "\\{([\\w.-]+)(?:, *([\\w.-]+))*\\}";

        final Pattern REGEX_PATTERN = Pattern.compile(VALUES_REGEX);
        final String values = "{df1_apx.fhh.irtrs.d.rrr, ffd1-afp.farr.d.rrr.asgd, ffd2-afp.farr.d.rrr.asgd}";
        final Matcher matcher = REGEX_PATTERN.matcher(values);
        if (null != values && matcher.matches()) {
            // for (int index=1; index<=matcher.groupCount(); ++index) {
            // System.out.println(matcher.group(index));
            // }

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

    }
}

I tried following combinations:

A) Regex as "^\{([0-9a-zA-Z\-\_\.]+)(?:,\s*([0-9a-zA-Z\-\_\.]))\}$" and use groupCount() to iterate. Result:

df1_apx.fhh.irtrs.d.rrr

ffd2-afp.farr.d.rrr.asgd

B) Regex as ^\{([0-9a-zA-Z\-\_\.]+)(?:,\s*([0-9a-zA-Z\-\_\.]))\}$" and use matcher.find(). Result: No result.

C) Regex as "\{([\w.-]+)(?:, ([\w.-]+))\}" and use groupCount() to iterate. Result:

df1_apx.fhh.irtrs.d.rrr

ffd2-afp.farr.d.rrr.asgd

D) Regex as "\{([\w.-]+)(?:, ([\w.-]+))\}" and use matcher.find(). Result: No results.

I never get consistent groups. Expected result here is:

df1_apx.fhh.irtrs.d.rrr

ffd1-afp.farr.d.rrr.asgd

ffd2-afp.farr.d.rrr.asgd

Please let me know, how can I achieve it.


Solution

  • (?<=[{,])\s*(.*?)(?=,|})
    

    You can simply use this and grab the captures.See demo.

    https://regex101.com/r/sJ9gM7/33

    When you have (#something)* then only the last group is remembered by the regex engine.You wont get all the groups this way.