Monday, November 28, 2011

Regular expressions simplify pattern-matching code - 24


PatternSyntaxException methods

Pattern's compilation methods throw PatternSyntaxException objects when they detect illegal syntax in a regex's pattern. An exception handler may call the following PatternSyntaxException methods to obtain information from a thrown PatternSyntaxException object about the syntax error:
  • public String getDescription(): returns the syntax error's description
  • public int getIndex(): returns either the approximate index (within a pattern) where the syntax error occurs or -1 if the index is unknown
  • public String getMessage(): builds a multiline string that contains the combined information the other three methods return along with a visual indication of the syntax error position within the pattern
  • public String getPattern(): returns the erroneous regex pattern

Because PatternSyntaxException inherits from java.lang.RuntimeException, code doesn't need to specify an exception handler. This proves appropriate when regexes are known to have correct patterns. But when potential for bad pattern syntax exists, an exception handler is necessary. Thus, RegexDemo's source code (see Listing 1) includes try { ... } catch (ParseSyntaxException e) { ... }, which calls each of the four previous PatternSyntaxException methods to obtain information about an illegal pattern.
What constitutes an illegal pattern? Not specifying the closing parentheses metacharacter in an embedded flag expression represents one example. Suppose you execute java RegexDemo (?itree Treehouse. That command line's illegal (?tree pattern causes p = Pattern.compile (args [0]); to throw a PatternSyntaxException object. You then observe the following output:
Regex syntax error: Unknown inline modifier near index 3
(?itree
   ^
Error description: Unknown inline modifier
Error index: 3
Erroneous pattern: (?itree

Note
The public PatternSyntaxException(String desc, String regex, int index) constructor lets you create your own PatternSyntaxException objects. That constructor comes in handy should you ever create your own preprocessing compilation method that recognizes your own pattern syntax, translates that syntax to syntax recognized by Pattern's compilation methods, and calls one of those compilation methods. If your method's caller violates your custom pattern syntax, you can throw an appropriate PatternSyntaxException object from that method.

No comments: