Java" PatternSyntaxException"非法/不支持的转义序列

时间:2017-08-02 15:38:00

标签: java regex parsing

我有下面这段代码,我试图让模式匹配器匹配像" .I 156" (完全是字符.I(点和我),然后是空格,然后是整数)

 while (currLine != null) {
        // Check if current line holds the query ID
        String regexp="\\.\\I\\s\\d";
        Pattern pattern=Pattern.compile(regexp);
        Matcher matcher;
        if (pattern.matcher(currLine).matches()) {
            queryBuffer.append(currLine);
            currLine = buffR.readLine();
            queryBuffer.append(".").append(" ").append(currLine);

            QueryListModel.add(iterator, queryBuffer.toString());
            queryBuffer.delete(0, queryBuffer.length());
            iterator++;
        }

        currLine = buffR.readLine();
    }

我从regexplanet.com上看了上面显示的正则表达式,我测试了它并验证了它。我检查了其他网站并进行了验证。 但是,在Eclipse控制台中,我收到以下错误:

Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 3

\.\I\s\d
   ^

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

  String regexp="\\.I\\s\\d");

这将匹配以.I开头的所有内容,并且它被一个数字所吞噬,并且它们之间只有1个空格 “.I 12132”或“.I 123213213”