Code:
package org.code2care.example;
public class PatternSyntaxExceptionExample {
public static void main(String[] args) {
String string = "1(2(3(4(5";
String[] strArr = string.split("(");
}
}
Exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1
(
at java.base/java.util.regex.Pattern.error(Pattern.java:2028)
at java.base/java.util.regex.Pattern.accept(Pattern.java:1878)
at java.base/java.util.regex.Pattern.group0(Pattern.java:3053)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2124)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2069)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1783)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1430)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1069)
at java.base/java.lang.String.split(String.java:3151)
at java. base/java.lang.String.split(String.java:3197)
at org.code2care.example.JavaSplitOnDotStringExample2.main(JavaSplitOnDotStringExample2.java:7)
Reason for the exception:
The java.lang.String split() function throws a PatternSyntaxException if the if the regular expression's syntax is invalid.
If you take a closer look at the regular expression used here open braces (, this has a special meaning when working with Regular Expressions. Such characters need to be escaped with a double forward slash \\
Fix:
Change the regex string from,
string.split("(");
to,
string.split("\\(");
Below is the list of all the characters that when you want to use "literally" in the regex string needs to be escaped.
Character | Description |
---|---|
\ | Escape character |
. | Any character except a newline |
+ | One or more occurrences |
* | Zero or more occurrences |
? | Zero or one occurrence |
` | ` |
( | Start of a capturing group |
) | End of a capturing group |
[ | Start of a character class |
] | End of a character class |
{ | Start of a quantifier |
} | End of a quantifier |
- | Range of characters in a class |
^ | Start of a string/line |
$ | End of a string/line |
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Cannot open or preview pdf with view only and restricted download access in Microsoft Teams - Teams
- Fix SharePoint 2019 installation error This product requires Visual C++ Redistributable Package for Visual Studio 2017 - SharePoint
- How to know the current version of Java - Java
- How to install Postman natively on a Mac - HowTos
- How to Manage Profile Picture on Microsoft Teams - Teams
- Spring Boot JDBCTemplate Upsert Example (batch insert or update if exists) - Java
- How to display date and time in GMT Timezone in Java - Java
- bash: netstat: command not found - Bash