Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.parse("(") error

Tags:

java

regex

split

I have a file which contains data as follows:

recv(1178884, NULL, 4294967267, 0)      = 0 ...... ...... 

My source code is:

try (BufferedReader br = new BufferedReader(new FileReader("D:\\smsTrace.txt"))) {     String sCurrentLine;      while ((sCurrentLine = br.readLine()) != null) {         String sysCallName = sCurrentLine;         String[] sysCallTokens = sysCallName.split("(");         System.out.println(sCurrentLine);     } } catch (IOException e) {     e.printStackTrace(); }  

When I split with sysCallName.split(",") it works fine but when I use as above, it throws following exception.

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1 (  ^     at java.util.regex.Pattern.error(Unknown Source)     at java.util.regex.Pattern.accept(Unknown Source)     at java.util.regex.Pattern.group0(Unknown Source)     at java.util.regex.Pattern.sequence(Unknown Source)     at java.util.regex.Pattern.expr(Unknown Source)     at java.util.regex.Pattern.compile(Unknown Source)     at java.util.regex.Pattern.<init>(Unknown Source)     at java.util.regex.Pattern.compile(Unknown Source)     at java.lang.String.split(Unknown Source)     at java.lang.String.split(Unknown Source)     at fileReading.main(fileReading.java:19) 

Any idea what I am doing wrong?

like image 695
Junaid Avatar asked Dec 19 '12 09:12

Junaid


People also ask

How do I fix parse error in Python?

To fix this, simply add the missing code, whether it is a single line or multiple lines. This code will fix the error, and the program will run properly. The error is removed by adding a single statement of the print() function. We can also use the pass keyword if we don't wish to execute anything.

What is a parse error in Python?

Syntax errors often called as parsing errors, are predominantly caused when the parser detects a syntactic issue in your code.

What is parsing error in JavaScript?

In JavaScript, when passing JSON to the JSON. parse() method, the method expects properly formatted JSON as the first argument. When it detects invalid JSON, it throws a JSON Parse error.

What error does JSON parse () throw when the string to parse is not valid JSON?

Exceptions. Throws a SyntaxError exception if the string to parse is not valid JSON.


2 Answers

You have to escape the opening bracket:

sysCallName.split("\\("); 

Because split() expects a regular expression, and brackets are used to mark capturing groups in a regex. So they need to be in pairs. If you just want a bracket it needs to be escaped.

like image 109
jlordo Avatar answered Sep 21 '22 14:09

jlordo


String#split takes a regular expression as splitting criteria. Now, ( in regex is used for capturing groups, so you need to escape it, if you want to match it.

You have two ways: -

  1. Escape the bracket: -

    sysCallName.split("\\("); 
  2. Use Character class: -

    sysCallName.split("[(]"); 

In a character class, all the meta-characters, loose their special meaning, so a ( is just a (, and a dot(.) is just a dot(.). Personally, I prefer the 2nd option, as it looks more clear.

like image 36
Rohit Jain Avatar answered Sep 22 '22 14:09

Rohit Jain