Java: Regex not matching

家住魔仙堡 提交于 2019-12-11 02:49:51

问题


I have comma separated string values. Every string may contain characters or numbers along with '-' or '/' or '.'.

My code looks like:

final String VALUES_REGEX = "^\\{([0-9a-zA-Z\\-\\_\\.])+,*([0-9a-zA-Z\\-\\_\\.])*\\}$";
final Pattern REGEX_PATTERN = Pattern.compile(VALUES_REGEX);
final String values = "{df1_apx.fhh.irtrs.d.rrr, ffd1-afp.farr.d.rrr.asgd}";
final Matcher matcher = REGEX_PATTERN.matcher(values);
if (null != values && matcher.matches()) {
    // further logic
}
...
...

Here if condition always returns false value because regex match fails. I verified regex using regexper. It looks fine.

Can you please tell me what is wrong here?

Update: With regex provided by Avinash, match works. But finding of groups fails. Code looks like:

final String VALUES_REGEX = "^\\{([0-9a-zA-Z\\-\\_\\.])+,*\\s*([0-9a-zA-Z\\-\\_\\.])*\\}$";
final Pattern REGEX_PATTERN = Pattern.compile(VALUES_REGEX);
final String values = "{df1_apx.fhh.irtrs.d.rrr, ffd1-afp.farr.d.rrr.asgd}";
final Matcher matcher = REGEX_PATTERN.matcher(values);
if (null != values && matcher.matches()) {
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
}
...
...

Update: After new regex provided by Avinash, tried to find separate groups. But comma is also considered as part of string. Code looks like:

final String VALUES_REGEX = "^\\{([0-9a-zA-Z\\-\\_\\.]+)((?:,\\s*[0-9a-zA-Z\\-\\_\\.]*)*)\\}$";
final Pattern REGEX_PATTERN = Pattern.compile(VALUES_REGEX);
final String values = "{df1_apx.fhh.irtrs.d.rrr, ffd1-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));
    }
}
...
...

Output is:

df1_apx.fhh.irtrs.d.rrr

, ffd1-afp.farr.d.rrr.asgd

I need to find only matching string values.


回答1:


There was a space after comma in your input string.

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



回答2:


You could simplify your regex as follows:

// [a-zA-Z0-9_] -> \w
final String VALUES_REGEX = "\\{([\\w.-]+)(?:, *([\\w.-]+))*\\}";

Note: ^ and $ are not necessary if you use Matcher.matches because it matches the entire string.

Edit: Regex updated to match the groups individually.

for (int i=1; i<=matcher.groupCount(); ++i) System.out.println(matcher.group(i));

Note: The repeated capturing group in the previous example will capture the last match only, so if you apply the pattern to the value "{first, second, third}" the result of the for-loop will give you first and third only.

Since this might not be what you've expected consier other possibilities, like searching for the values only (here no separater checks are made):

final Matcher matcher = Pattern.compile("[\\w.-]+").matcher(values);
while (matcher.find()) System.out.println(matcher.group());

or just splitting the string as well.

String[] strings = values.substring(1, values.length() - 2).split(", *");
System.out.println(Arrays.toString(strings));



回答3:


IntelliJ is able to match your example strings using this regex:

^([0-9a-zA-Z_\-\.])+([,]*)([0-9a-zA-Z_\-\.])*$


来源:https://stackoverflow.com/questions/29342208/java-regex-not-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!