未公开的字符类错误? [英] Unclosed Character Class Error?

查看:95
本文介绍了未公开的字符类错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是错误:

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?

推荐答案

字符串#split 适用于正则表达式 [(和] )不是标准字符,正则表达式。因此,他们需要转义 - 使用 \ [(和 \] )。

String#split works with a Regular Expression but [ (and ]) are not standard characters, "regex-wise". So, they need to be escaped — using \[ (and \]).

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

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

因此,只是为了拆分 [,使用的Java 字符串\\ [;并且您正试图获得:

Thus, just to split on [, the Java String used is "\\["; and you are trying to obtain:

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

这篇关于未公开的字符类错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆