简体   繁体   中英

Java: Regex not matching

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.

输入字符串中逗号后有空格。

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

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));

IntelliJ可以使用此正则表达式匹配示例字符串:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM