[英]Java Regex Unclosed character class (PatternSyntaxException)
I am having trouble compiling a regex. 我在编译正则表达式时遇到了麻烦。 I cant find what the problem is with this expression as I got it from Cisco documentation and I don't understand why it does not work.
当我从思科文档中获得该表达式时,我找不到该表达式的问题,而且我不明白为什么它不起作用。 I'm hoping that somebody could tell me what is wrong with it.
我希望有人能告诉我这是怎么回事。 This is what I am trying to do:
这就是我想要做的:
public void test(){
try{
pattern.compile("^[]0-9*#X[^-]{1,50}$");
System.out.println("Syntax is ok");
} catch (PatternSyntaxException e) {
System.out.println(e.getDescription());
}
}
This: 这个:
^[]0-9*#X[^-]{1,50}$
Doesn't work, you have to replace []0-9
with [0-9]
: 不起作用,您必须将
[]0-9
替换为[0-9]
:
^[0-9]*#X[^-]{1,50}$
UPDATE UPDATE
As Duncan Jones says, maybe you wanted to match []
at the beginning of the string. 正如邓肯·琼斯(Duncan Jones)所说,也许您想在字符串的开头匹配
[]
。 In this case, you regex has to become 在这种情况下,您的正则表达式必须变为
^\[\]0-9*#X[^-]{1,50}$
So: 所以:
pattern.compile("^\\[\\]0-9*#X[^-]{1,50}$");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.