正则表达式错误验证

时间:2015-09-04 13:40:37

标签: java regex

任何人都可以解释为什么这个正则表达式没有抓住输入。我需要捕捉正则表达式中提到的特殊字符。

        final String REGEX="[.,%*$#@?^<!&>'|/\\\\~\\[\\]{}+=\"-]*";


        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher("2c450807-4a4c-4f18-bf4f-5a100ced87a0");

        if (matcher.matches()) {

            System.out.println("found");

        }
        else{

            System.out.println("not found!");
        }

这会打印“未找到!”。请帮帮我。

1 个答案:

答案 0 :(得分:1)

而不是matcher.matches()使用:

matcher.find()

matcher.matches()期望匹配完整输入。

使用此正则表达式也更好:

final String REGEX="[.,%*$#@?^<!&>'|/\\\\~\\[\\]{}+=\"-]";

*置于前面会使其与空字符串匹配。

相关问题