Java 未关闭的字符类错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21816788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:50:44  来源:igfitidea点击:

Unclosed Character Class Error?

javaregexsplit

提问by ylun.ca

Here is the error:

这是错误:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 3
], [
   ^
    at java.util.regex.Pattern.error(Pattern.java:1924)
    at java.util.regex.Pattern.clazz(Pattern.java:2493)
    at java.util.regex.Pattern.sequence(Pattern.java:2030)
    at java.util.regex.Pattern.expr(Pattern.java:1964)
    at java.util.regex.Pattern.compile(Pattern.java:1665)
    at java.util.regex.Pattern.<init>(Pattern.java:1337)
    at java.util.regex.Pattern.compile(Pattern.java:1022)
    at java.lang.String.split(String.java:2313)
    at java.lang.String.split(String.java:2355)
    at testJunior2013.J2.main(J2.java:31)

This is the area of the code that is causing the issues.

这是导致问题的代码区域。

String[][] split = new String[1][rows];

split[0] = (Arrays.deepToString(array2d)).split("], ["); //split at the end of an array row

What does this error mean and what needs to be done to fix the code above?

这个错误是什么意思,需要做什么来修复上面的代码?

采纳答案by ccjmne

TL;DR

TL; 博士

You want:

你要:

.split("\], \[")`

Escapeeach square bracket twice— once for each context in which you need to strip them from their special meaning: within a Regular Expressionfirst, and within a Java Stringsecondly.

每个方括号进行两次转义— 对于需要将它们从其特殊含义中剥离的每个上下文一次:首先是在正则表达式中,其次是在Java 字符串中

Consider using Pattern#quotewhen you need your entire patternto be interpreted literally.

Pattern#quote当您需要按字面解释整个模式时,请考虑使用。



Explanation

解释

String#splitworks with a Regular Expressionbut [and ]are not standard characters, regex-wise: they have a special meaning in that context.

String#split有工作正则表达式,但[]没有标准字符,正则表达式明智的:他们在这方面有特殊的意义。

In order to strip them from their special meaningand simply match actual square brackets, they need to be escaped, which is done by preceding each with a backslash— that is, using \[and \].

为了将它们从它们的特殊含义中剥离出来并简单地匹配实际的方括号,它们需要被转义,这是通过在每个前面加上一个反斜杠来完成的——即使用\[and \]

However, in a Java String, \is not a standard character either, and needs to be escaped as well.

但是,在Java String 中\也不是标准字符,也需要转义

Thus, just to split on [, the Stringused is "\\["and you are trying to obtain:

因此,只是为了拆分[,使用的字符串"\\[",您正在尝试获得:

.split("\], \[")


A sensible alternative

一个明智的选择

However, in this case, you're not just semantically escaping a few specific characters in a Regular Expression, but actually wishing that your entire pattern be interpreted literally: there's a method to do just that

但是,在这种情况下,您不仅在语义上对正则表达式中的几个特定字符进行了转义,而且实际上希望您的整个模式都能按字面意思进行解释:有一种方法可以做到这一点

Pattern#quoteis used to signify that the:

Pattern#quote用于表示:

Metacharacters [...] in your pattern will be given no special meaning.

模式中的元字符 [...] 没有特殊含义。

(from the Javadoc linked above)

(来自上面链接的Javadoc)

I recommend, in this case, that you use the following, more sensible and readable:

在这种情况下,我建议您使用以下更明智和可读的内容:

.split(Pattern.quote("], ["))

回答by xp500

Split receives a regex and [, ] characters have meaning in regex, so you should escape them with \\[and \\].

Split 接收一个正则表达式,而 [, ] 字符在正则表达式中有意义,所以你应该用\\[和将它们转义\\]

The way you are currently doing it, the parser finds a ] without a preceding [ so it throws that error.

按照您目前的做法,解析器会找到一个没有前面 [ 的 ],因此它会抛出该错误。

回答by Marc B

  .split("], [")
             ^---start of char class
                  end----?

Change it to

将其更改为

.split("], \[")
           ^---escape the [

回答by McLovin

String.split() takes a regular expression, not a normal string as an argument. In a regular expression, ] and [ are special characters, which need to be preceded by backslashes to be taken literally. Use .split("\\], \\["). (the double backslashes tell Java to interpret the string as "\], \[").

String.split() 接受一个正则表达式,而不是一个普通的字符串作为参数。在正则表达式中,] 和 [ 是特殊字符,需要在前面加上反斜杠才能按字面意思理解。使用.split("\\], \\["). (双反斜杠告诉 Java 将字符串解释为“\]、\[”)。

回答by Skizzo

Try to use it

尝试使用它

 String stringToSplit = "8579.0,753.34,796.94,\"[784.2389999999999,784.34]\",\"[-4.335912230999999, -4.3603307895,4.0407909059, 4.08669583455]\",[],[],[],0.1744,14.4,3.5527136788e-15,0.330667850653,0.225286999939,Near_Crash";
 String [] arraySplitted = stringToSplit.replaceAll("\"","").replaceAll("\[","").replaceAll("\]","").trim().split(",");