sunlabs.brazil.util.regexp
Interface Regexp.Filter

Enclosing class:
Regexp

public static interface Regexp.Filter

This interface is used by the Regexp class to generate the replacement string for each pattern match found in the source string.


Method Summary
 boolean filter(Regsub rs, StringBuffer sb)
          Given the current state of the match, generate the replacement string.
 

Method Detail

filter

boolean filter(Regsub rs,
               StringBuffer sb)
Given the current state of the match, generate the replacement string. This method will be called for each match found in the source string, unless this filter decides not to handle any more matches.

The implementation can use whatever rules it chooses to generate the replacement string. For example, here is an example of a filter that replaces the first 5 occurrences of "%XX" in a string with the ASCII character represented by the hex digits "XX":

 String str = ...;

 Regexp re = new Regexp("%[a-fA-F0-9][a-fA-F0-9]");

 Regexp.Filter rf = new Regexp.Filter() {
     int count = 5;
     public boolean filter(Regsub rs, StringBuffer sb) {
         String match = rs.matched();
         int hi = Character.digit(match.charAt(1), 16);
         int lo = Character.digit(match.charAt(2), 16);
         sb.append((char) ((hi << 4) | lo));
         return (--count > 0);
     }
 }

 String result = re.sub(str, rf);
 

Parameters:
rs - Regsub containing the state of the current match.
sb - The string buffer that this filter should append the generated string to. This string buffer actually contains the results the calling Regexp has generated up to this point.
Returns:
false if no further matches should be considered in this string, true to allow Regexp to continue looking for further matches.

Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.