The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Bonus
Lesson: Regular Expressions

Methods of the PatternSyntaxException Class

A PatternSyntaxException (in the API reference documentation) is an unchecked exception that indicates a syntax error in a regular expression pattern. The PatternSyntaxException class provides the following methods to help you determine what went wrong: The following source code, RegexTestHarness2 (in a .java source file), updates our test harness to check for malformed regular expressions. It intentionally introduces a syntax error, then examines the caught exception using the methods listed above. Changes are marked in bold.
import java.io.*;
import java.util.regex.*;

public final class RegexTestHarness2 {

    private static String REGEX;
    private static String INPUT;
    private static BufferedReader br;
    private static Pattern pattern;
    private static Matcher matcher;
    private static boolean found;

    public static void main(String[] argv) {
        initResources();
        processTest();
        closeResources();
    }

    private static void initResources() {
       try {
           br = new BufferedReader(new FileReader("regex.txt"));
       }
       catch (FileNotFoundException fnfe) {
            System.out.println("Cannot locate input file! "+fnfe.getMessage());
            System.exit(0);
        }
       try {
           REGEX = br.readLine();
           INPUT = br.readLine();
       } catch (IOException ioe) {}

       try {
        pattern = Pattern.compile(REGEX);
        matcher = pattern.matcher(INPUT);
       } 
       catch(PatternSyntaxException pse) {
           System.out.println("There is a problem with the regular expression!");
           System.out.println("The pattern in question is: "+pse.getPattern());
           System.out.println("The description is: "+pse.getDescription());
           System.out.println("The message is: "+pse.getMessage());
           System.out.println("The index is: "+pse.getIndex());
           System.exit(0);
        }

        System.out.println("Current REGEX is: "+REGEX);
        System.out.println("Current INPUT is: "+INPUT);
    }

    private static void processTest() {
        while(matcher.find()) {
            System.out.println("I found the text \"" + matcher.group() +
                               "\" starting at index " + matcher.start() +
                               " and ending at index " + matcher.end() + ".");
            found = true;
        }

        if(!found){
            System.out.println("No match found.");
        }
    }

    private static void closeResources() {
       try{
           br.close();
       }catch(IOException ioe){}
    }
}
To run this test, change the file regex.txt to contain ?i)foo as its first line, and FoO as its second line. This mistake is a common scenario in which the programmer has forgotten the opening parenthesis in the embedded flag expression (?i).
OUTPUT:

There is a problem with the regular expression!
The pattern in question is: ?i)foo
The description is: Dangling meta character '?'
The message is: Dangling meta character '?' near index 0 
?i)foo
^
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.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.