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?
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.
Syntax errors often called as parsing errors, are predominantly caused when the parser detects a syntactic issue in your code.
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.
Exceptions. Throws a SyntaxError exception if the string to parse is not valid JSON.
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.
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: -
Escape the bracket: -
sysCallName.split("\\(");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With