Package sunlabs.brazil.util.regexp
Interface Regexp.Filter
-
- Enclosing class:
- Regexp
public static interface Regexp.Filter
This interface is used by theRegexp
class to generate the replacement string for each pattern match found in the source string.- Version:
- 2.3, 04/12/30
- Author:
- Colin Stevens (colin.stevens@sun.com)
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description boolean
filter(Regsub rs, java.lang.StringBuffer sb)
Given the current state of the match, generate the replacement string.
-
-
-
Method Detail
-
filter
boolean filter(Regsub rs, java.lang.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 callingRegexp
has generated up to this point.- Returns:
false
if no further matches should be considered in this string,true
to allowRegexp
to continue looking for further matches.
-
-