Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java parsing Error

Tags:

java

parsing

I was trying to parse the string:

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}--]";

with delimiter as [

and my code was

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

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

but it is giving me error

java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [
like image 626
Mohit BAnsal Avatar asked Dec 05 '25 21:12

Mohit BAnsal


2 Answers

the [ character has special meanings in regular expressions. You need to do

String delims = "\\["; String[] tokens = s.split(delims);
like image 91
Rob Di Marco Avatar answered Dec 07 '25 10:12

Rob Di Marco


As Rob Di Marco already said, you have to escape characters with special meanings when using a regex based method.

As an alternative, you could use StringTokenizer, which does not use regexes to split and thus you don't have to escape anything. This is also usually faster but cannot match the power and flexibility of the regex based split.

StringTokenizer tok=new StringTokenizer(s, "[");
List<String> tokens=new ArrayList<String>();
while (tok.hasMoreTokens()){
   tokens.add(tok.nextToken());
}
like image 27
MAK Avatar answered Dec 07 '25 09:12

MAK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!