public class Replacer extends Object implements Serializable
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]"
This code was mostly written in 2001, I hope the reference isn't too outdated...| Constructor and Description |
|---|
Replacer(Pattern pattern,
String substitution)
Constructs a Replacer from a Pattern and a String to replace occurrences of the Pattern with.
|
Replacer(Pattern pattern,
String substitution,
boolean isPerlExpr) |
Replacer(Pattern pattern,
Substitution substitution)
Unlikely to be used directly.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object o) |
int |
hashCode() |
String |
replace(char[] chars,
int off,
int len) |
int |
replace(char[] chars,
int off,
int len,
StringBuilder sb) |
void |
replace(char[] chars,
int off,
int len,
Writer out) |
String |
replace(CharSequence text)
Takes all instances in text of the Pattern this was constructed with, and replaces them with substitution.
|
int |
replace(CharSequence text,
StringBuilder sb)
Takes all occurrences of the pattern this was constructed with in text and replaces them with the substitution.
|
int |
replace(CharSequence text,
TextBuffer dest) |
void |
replace(CharSequence text,
Writer out) |
static int |
replace(Matcher m,
Substitution substitution,
TextBuffer dest)
Replaces all occurrences 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. |
static int |
replace(Matcher m,
Substitution substitution,
TextBuffer dest,
int count)
Replaces the first n occurrences of a matcher's pattern, where n is equal to count,
in a matcher's target by a given substitution, appending the result to a buffer.
|
String |
replace(MatchResult res,
int group) |
int |
replace(MatchResult res,
int group,
StringBuilder sb) |
void |
replace(MatchResult res,
int group,
Writer out) |
int |
replace(MatchResult res,
String groupName,
StringBuilder sb) |
void |
replace(MatchResult res,
String groupName,
Writer out) |
String |
replace(Reader text,
int length) |
int |
replace(Reader text,
int length,
StringBuilder sb) |
void |
replace(Reader in,
int length,
Writer out) |
void |
setSubstitution(String s,
boolean isPerlExpr) |
String |
toString() |
static TextBuffer |
wrap(StringBuilder sb) |
public Replacer(Pattern pattern, Substitution substitution)
pattern - a regexodus.Pattern that determines what should be replacedsubstitution - an implementation of the Substitution interface, which allows custom replacement behaviorpublic Replacer(Pattern pattern, String substitution)
pattern - a regexodus.Pattern that determines what should be replacedsubstitution - a String that will be used to replace occurrences of the Patternpublic void setSubstitution(String s, boolean isPerlExpr)
public String replace(CharSequence text)
text - a String, StringBuilder, or other CharSequence that may contain the text to replacepublic String replace(MatchResult res, int group)
public String replace(Reader text, int length) throws IOException
IOExceptionpublic int replace(CharSequence text, StringBuilder sb)
text - a String, StringBuilder, or other CharSequence that may contain the text to replacesb - the StringBuilder to append the result intopublic int replace(char[] chars, int off, int len, StringBuilder sb)
public int replace(MatchResult res, int group, StringBuilder sb)
public int replace(MatchResult res, String groupName, StringBuilder sb)
public int replace(Reader text, int length, StringBuilder sb) throws IOException
IOExceptionpublic int replace(CharSequence text, TextBuffer dest)
public static int replace(Matcher m, Substitution substitution, TextBuffer dest)
public static int replace(Matcher m, Substitution substitution, TextBuffer dest, int count)
public void replace(CharSequence text, Writer out) throws IOException
IOExceptionpublic void replace(char[] chars, int off, int len, Writer out) throws IOException
IOExceptionpublic void replace(MatchResult res, int group, Writer out) throws IOException
IOExceptionpublic void replace(MatchResult res, String groupName, Writer out) throws IOException
IOExceptionpublic void replace(Reader in, int length, Writer out) throws IOException
IOExceptionpublic static TextBuffer wrap(StringBuilder sb)
Copyright © 2016. All rights reserved.