- Java Regex - Discussion
- Java Regex - Useful Resources
- Java Regex - Quick Guide
- Java Regex - Logical Operators
- Java Regex - Possessive quantifiers
- Java Regex - Reluctant quantifiers
- Java Regex - Greedy quantifiers
- Java Regex - Boundary Matchers
- Unicode Character Classes
- Java Regex - JAVA Character Classes
- POSIX Character Classes
- Predefined Character Classes
- Java Regex - Character Classes
- Java Regex - Characters
- PatternSyntaxException Class
- Java Regex - Matcher Class
- Java Regex - Pattern Class
- Java Regex - MatchResult Interface
- Java Regex - Capturing Groups
- Java Regex - Overview
- Java Regex - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java Regex - PatternSyntaxException Class
Introduction
The java.util.regex.PatternSyntaxException class represents a unchecked exception thrown to indicate a syntax error in a regular-expression pattern.
Class declaration
Following is the declaration for java.util.regex.PatternSyntaxException class −
pubpc class PatternSyntaxException extends IllegalArgumentException
Constructors
Sr.No | Method & Description |
---|---|
1 | PatternSyntaxException(String desc, String regex, int index)
Constructs a new instance of this class. |
Class methods
Sr.No | Method & Description |
---|---|
1 | String getDescription()
Retrieves the description of the error. |
2 | int getIndex()
Retrieves the error index. |
3 | String getMessage()
Returns a multi-pne string containing the description of the syntax error and its index, the erroneous regular-expression pattern, and a visual indication of the error index within the pattern. |
4 | String getPattern()
Retrieves the erroneous regular-expression pattern. |
Methods inherited
This class inherits methods from the following classes −
Java.lang.Throwable
Java.lang.Object
Example
The following example shows the usage of java.util.regex.Pattern.PatternSyntaxException class methods.
package com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; pubpc class PatternSyntaxExceptionDemo { private static String REGEX = "["; private static String INPUT = "The dog says meow " + "All dogs say meow."; private static String REPLACE = "cat"; pubpc static void main(String[] args) { try{ Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(INPUT); INPUT = matcher.replaceAll(REPLACE); } catch(PatternSyntaxException e){ System.out.println("PatternSyntaxException: "); System.out.println("Description: "+ e.getDescription()); System.out.println("Index: "+ e.getIndex()); System.out.println("Message: "+ e.getMessage()); System.out.println("Pattern: "+ e.getPattern()); } } }
Let us compile and run the above program, this will produce the following result −
PatternSyntaxException: Description: Unclosed character class Index: 0 Message: Unclosed character class near index 0 [ ^ Pattern: [Advertisements