开发者

Why is this exception thrown when trying to match this regex in Java?

I'm trying to match a specific string out of a an H开发者_运维知识库TML document and have this regex pattern to grab it:

Pattern somePattern = Pattern.compile("var json = ({\"r\":\"^d1\".*});");

However when I try to hit that code at runtime, I get this error:

FATAL EXCEPTION: Timer-0
 java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX near index 13:
 var json = ({"r":"^d1".*});
              ^
     at com.ibm.icu4jni.regex.NativeRegEx.open(Native Method)
     at java.util.regex.Pattern.compileImpl(Pattern.java:383)
     at java.util.regex.Pattern.<init>(Pattern.java:341)
     at java.util.regex.Pattern.compile(Pattern.java:317)

Can anybody tell me what I'm doing wrong?


I think you need to escape the "{}" brace characters as these mean something special to regex.

This is the long hand way of expressing a "count" so.{0,}is equivalent to.* .{0,1}is equivalent to.?and.{2,4}means at least two but no more than four of the previous match

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜