| java.lang.Object jregex.Replacer
Replacer | public class Replacer (Code) | | The Replacer class suggests some methods to replace occurences of a pattern
either by a result of evaluation of a perl-like expression, or by a plain string,
or according to a custom substitution model, provided as a Substitution interface implementation.
A Replacer instance may be obtained either using Pattern.replacer(...) method, or by constructor:
Pattern p=new Pattern("\\w+");
Replacer perlExpressionReplacer=p.replacer("[$&]");
//or another way to do the same
Substitution myOwnModel=new Substitution(){
public void appendSubstitution(MatchResult match,TextBuffer tb){
tb.append('[');
match.getGroup(MatchResult.MATCH,tb);
tb.append(']');
}
}
Replacer myVeryOwnReplacer=new Replacer(p,myOwnModel);
The second method is much more verbose, but gives more freedom.
To perform a replacement call replace(someInput):
System.out.print(perlExpressionReplacer.replace("All your base "));
System.out.println(myVeryOwnReplacer.replace("are belong to us"));
//result: "[All] [your] [base] [are] [belong] [to] [us]"
See Also: Substitution See Also: PerlSubstitution See Also: Replacer.Replacer(jregex.Patternjregex.Substitution) |
Method Summary | |
public String | replace(String text) | public String | replace(char[] chars, int off, int len) | public String | replace(MatchResult res, int group) | public String | replace(Reader text, int length) | public int | replace(String text, StringBuffer sb) | public int | replace(char[] chars, int off, int len, StringBuffer sb) | public int | replace(MatchResult res, int group, StringBuffer sb) | public int | replace(MatchResult res, String groupName, StringBuffer sb) | public int | replace(Reader text, int length, StringBuffer sb) | public int | replace(String text, TextBuffer dest) | public int | replace(char[] chars, int off, int len, TextBuffer dest) | public int | replace(MatchResult res, int group, TextBuffer dest) | public int | replace(MatchResult res, String groupName, TextBuffer dest) | public int | replace(Reader text, int length, TextBuffer dest) | public static int | replace(Matcher m, Substitution substitution, TextBuffer dest) Replaces all occurences of a matcher's pattern in a matcher's target
by a given substitution appending the result to a buffer.
The substitution starts from current matcher's position, current match
not included. | public static int | replace(Matcher m, Substitution substitution, Writer out) | public void | replace(String text, Writer out) | public void | replace(char[] chars, int off, int len, Writer out) | public void | replace(MatchResult res, int group, Writer out) | public void | replace(MatchResult res, String groupName, Writer out) | public void | replace(Reader in, int length, Writer out) | public void | setSubstitution(String s, boolean isPerlExpr) | public static TextBuffer | wrap(StringBuffer sb) | public static TextBuffer | wrap(Writer writer) |
replace | public String replace(char[] chars, int off, int len)(Code) | | |
replace | public int replace(char[] chars, int off, int len, TextBuffer dest)(Code) | | |
replace | public static int replace(Matcher m, Substitution substitution, TextBuffer dest)(Code) | | Replaces all occurences of a matcher's pattern in a matcher's target
by a given substitution appending the result to a buffer.
The substitution starts from current matcher's position, current match
not included.
|
setSubstitution | public void setSubstitution(String s, boolean isPerlExpr)(Code) | | |
|
|