日期:2014-05-20  浏览次数:20816 次

java正则表达式抛出异常求教
Pattern pattern = Pattern.compile("<p node-type=\\");
抛出异常
java.util.regex.PatternSyntaxException: Unexpected internal error near index 14
<p node-type=\
  ^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.compile(Pattern.java:1466)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
at sinaSpider.MyHttpClient.getUIDBlogs(MyHttpClient.java:164)
at sinaSpider.Crawler.main(Crawler.java:10)

我希望匹配的是
<p node-type=\

------解决方案--------------------
因为在字符串和正则中,\ 都是转义字符,为了完成两次转义,所以你不得不写成:

Pattern pattern = Pattern.compile("<p node-type=\\\\");