Java - PatternSyntaxException
PatternSyntaxException is a Runtime Java Exception that you may get while dealing with Regular Expressions in your program that is incorrect. Please make sure that your expression is valid.
Example:public static void main(String[] args) {
String dataString = "This.is.my.string.";
String[] dataStringArr = dataString.split("${\\.}");
for (String data : dataStringArr) {
System.out.println(data);
}
}
Output Error Stack Trace:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 1
\${\.}
^
at java.base/java.util.regex.Pattern.error(Pattern.java:2027)
at java.base/java.util.regex.Pattern.closure(Pattern.java:3320)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2213)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2068)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1782)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1428)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1068)
at java.base/java.lang.String.split(String.java:2317)
at java.base/java.lang.String.split(String.java:2364)
at com.code2care.java.tutorials.Sample.main(Sample.java:8)
The correct expression syntax here should be "\\." making sure to escape the dot with slashes.
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Java C# foreach loop equivalent in Python Programming - Python
- Remove AirDrop Icon from macOS Menu Bar - MacOS
- How to check if a variable is set in Bash Script or Not - Bash
- Vertical align two div's in Bootstrap [HTML CSS] - Bootstrap
- Amp Hello World Example - AMP
- What is exit(0), exit(1) ... exit(8) codes in Python Programming - Python
- Java 8: Convert Stream to Array - Java
- [Microsoft Teams 4c7] Modern authentication failed here, but you'll still be able to sign in. Your status code is 4c7 error - Microsoft