Java 解析错误

标签 java parsing

我试图解析字符串:

Portfolio1[{Exchange:NASDAQ-Symbol:INFY-Full Name:Infosys Technologies Limited (ADR)-Share Count:100.0-Percent Gain:388.2258065-The position cost is:1240.0 USD-This position made today:-46.9997 USD-This position has a total gain of:4814.0 USD-This position is worth:6054.0 USD}--{Exchange:NASDAQ-Symbol:GOOG-Full Name:Google Inc.-Share Count:10.0-Percent Gain:17.98444444-The position cost is:4500.0 USD-This position made today:-10.70007 USD-This position has a total gain of:809.3 USD-This position is worth:5309.3 USD}--{Exchange:NASDAQ-Symbol:QCOM-Full Name:QUALCOMM, Inc.-Share Count:0.0-Percent Gain:0.0}--]Portfolio2[{Exchange:NASDAQ-Symbol:GOOG-Full Name:Google Inc.-Share Count:1000.0-Percent Gain:32.56679151-The position cost is:400500.0 USD-This position made today:-1070.007 USD-This position has a total gain of:130430.0 USD-This position is worth:530930.0 USD}--{Exchange:NASDAQ-Symbol:MSFT-Full Name:Microsoft Corporation-Share Count:10.0-Percent Gain:-4.03125-The position cost is:320.0 USD-This position made today:-2.93001 USD-This position has a total gain of:-12.9 USD-This position is worth:307.1 USD}--{Exchange:NYSE-Symbol:IBM-Full Name:International Business Machines Corp.-Share Count:10.0-Percent Gain:397.80769230000004-The position cost is:260.0 USD-This position made today:-10.30014 USD-This position has a total gain of:1034.3 USD-This position is worth:1294.3 USD}--{Exchange:NASDAQ-Symbol:NVDA-Full Name:NVIDIA Corporation-Share Count:100.0-Percent Gain:-10.79418345-The position cost is:1788.0 USD-This position made today:-70.0 USD-This position has a total gain of:-193.0 USD-This position is worth:1595.0 USD}--]";

分隔符为[

我的代码是

String delims = "[";
String[] tokens = s.split(delims);

for (int i = 0; i < tokens.length; i++)
    System.out.println(tokens[i]);

但它给我错误

java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [

最佳答案

[ 字符在正则表达式中具有特殊含义。你需要做的

String delims = "\\["; String[] tokens = s.split(delims);

关于Java 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2746816/

相关文章:

Java日期字符串解析创建时区差异

java - 如何从渲染线程显示对话框?

Java正则表达式截断字符串

java - 从 Android 中的两个不同 Activity 打开时 Activity 的不同行为

java - 如何在 Java Web 应用程序中嵌入 DocBook XSLT 转换?

jquery - 使用 JQuery 提取 HTML

Java gRPC 服务器用于长生命周期流的有效实现

string - 检查字符串是否以十进制数字开头?

java - 将 HTTP Get 请求解析为组成字段

java - Java如何通过迭代器迭代XML文件?