Java Regex inconsistent groups -
please refer following question on so:
my regex groups not consistent. 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()) { // (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 "^\{([0-9a-za-z\-\_\.]+)(?:,\s*([0-9a-za-z\-\_\.]))\}$" , use groupcount() iterate. result:
df1_apx.fhh.irtrs.d.rrr ffd2-afp.farr.d.rrr.asgd b) regex ^\{([0-9a-za-z\-\_\.]+)(?:,\s*([0-9a-za-z\-\_\.]))\}$" , use matcher.find(). result: no result.
c) regex "\{([\w.-]+)(?:, ([\w.-]+))\}" , use groupcount() iterate. result:
df1_apx.fhh.irtrs.d.rrr ffd2-afp.farr.d.rrr.asgd d) regex "\{([\w.-]+)(?:, ([\w.-]+))\}" , use matcher.find(). result: no results.
i never 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 achieve it.
(?<=[{,])\s*(.*?)(?=,|}) you can use , grab captures.see demo.
https://regex101.com/r/sj9gm7/33
when have (#something)* last group remembered regex engine.you wont groups way.
Comments
Post a Comment