【发布时间】:2017-08-02 15:38:00
【问题描述】:
我有以下一段代码,我尝试在模式匹配器中匹配“.I 156”之类的字符串(确切地说是字符 .I(点和 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
^
任何帮助表示赞赏。
【问题讨论】:
-
您不必转义
I字母。 -
\\在I之前有什么意义? (没有,这就是您收到此错误的原因)如果您不从阅读有关它们的教程开始,请使用您理解的工具。 -
我从在线 reg exp builder 的复制粘贴中插入了 \\。显然我不知道它是不需要的,我认为来源是真实有效的,但事实并非如此。谢谢你的答案,我已经解决了!