KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > regex > PatternSyntaxException

java.util.regex
Class PatternSyntaxException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.lang.RuntimeException
              extended by java.lang.IllegalArgumentException
                  extended by java.util.regex.PatternSyntaxException
All Implemented Interfaces:
Serializable
See Also:
Top Examples, Source Code

public String getDescription()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1927]PatternSyntaxException
By Anonymous on 2007/08/15 11:29:57  Rate
A PatternSyntaxException is an unchecked exception that indicates a syntax error in a regular expression pattern.

public int getIndex()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getMessage()
See Also:
Throwable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1928]PatternSyntaxException.getMessage()
By Anonymous on 2007/08/15 11:30:35  Rate
Returns a multi-line 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.

public String getPattern()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public PatternSyntaxException(String desc,
                              String regex,
                              int index)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1929]How to use PatternSyntaxException
By Anonymous on 2007/08/15 11:31:41  Rate
import java.io.Console; 
     import java.util.regex.Pattern; 
     import java.util.regex.Matcher; 
     import java.util.regex.PatternSyntaxException; 
  
  
     public class RegexTestHarness2  {  
  
  
         public static void main ( String [  ]  args )  {  
             Pattern pattern = null; 
             Matcher matcher = null; 
  
  
             Console console = System.console (  ) ; 
             if  ( console == null )   {  
                 System.err.println ( "No console." ) ; 
                 System.exit ( 1 ) ; 
              }  
             while  ( true )   {  
                 try {  
                     pattern =  
                     Pattern.compile ( console.readLine ( "%nEnter your regex: " )  ) ; 
  
  
                     matcher =  
                     pattern.matcher ( console.readLine ( "Enter input string to search: " )  ) ; 
                  }  
                 catch ( PatternSyntaxException pse )  {  
                     console.format ( "There is a problem with the regular expression!%n" ) ; 
                     console.format ( "The pattern in question is: %s%n",pse.getPattern (  )  ) ; 
                     console.format ( "The description is: %s%n",pse.getDescription (  )  ) ; 
                     console.format ( "The message is: %s%n",pse.getMessage (  )  ) ; 
                     console.format ( "The index is: %s%n",pse.getIndex (  )  ) ; 
                     System.exit ( 0 ) ; 
                  }  
                 boolean found = false; 
                 while  ( matcher.find (  )  )   {  
                     console.format ( "I found the text \"%s\" starting at " + 
                        "index %d and ending at index %d.%n", 
                         matcher.group (  ) , matcher.start (  ) , matcher.end (  )  ) ; 
                     found = true; 
                  }  
                 if ( !found )  {  
                     console.format ( "No match found.%n" ) ; 
                  }  
              }  
          }  
      }  
  
  
  
 To run this test, enter ?i ) foo as the regular expression. This mistake is a common scenario in which the programmer has forgotten the opening parenthesis in the embedded flag expression  ( ?i ) . Doing so will produce the following results: 
  
  
     Enter your regex: ?i )  
     There is a problem with the regular expression! 
     The pattern in question is: ?i )  
     The description is: Dangling meta character '?' 
     The message is: Dangling meta character '?' near index 0 
     ?i )  
     ^ 
     The index is: 0 
  
  
 From this output, we can see that the syntax error is a dangling metacharacter  ( the question mark )  at index 0. A missing opening parenthesis is the culprit.

Popular Tags