Package totalcross.util.regex
Class Replacer
- java.lang.Object
-
- totalcross.util.regex.Replacer
-
public class Replacer extends java.lang.ObjectThe 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]"
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Stringreplace(char[] chars, int off, int len)intreplace(char[] chars, int off, int len, java.lang.StringBuffer sb)voidreplace(char[] chars, int off, int len, CharStream out)intreplace(char[] chars, int off, int len, TextBuffer dest)java.lang.Stringreplace(java.lang.String text)intreplace(java.lang.String text, java.lang.StringBuffer sb)voidreplace(java.lang.String text, CharStream out)intreplace(java.lang.String text, TextBuffer dest)java.lang.Stringreplace(CharStream text, int length)intreplace(CharStream text, int length, java.lang.StringBuffer sb)voidreplace(CharStream in, int length, CharStream out)intreplace(CharStream text, int length, TextBuffer dest)static intreplace(Matcher m, Substitution substitution, CharStream out)static intreplace(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.java.lang.Stringreplace(MatchResult res, int group)intreplace(MatchResult res, int group, java.lang.StringBuffer sb)voidreplace(MatchResult res, int group, CharStream out)intreplace(MatchResult res, int group, TextBuffer dest)intreplace(MatchResult res, java.lang.String groupName, java.lang.StringBuffer sb)voidreplace(MatchResult res, java.lang.String groupName, CharStream out)intreplace(MatchResult res, java.lang.String groupName, TextBuffer dest)voidsetSubstitution(java.lang.String s, boolean isPerlExpr)static TextBufferwrap(java.lang.StringBuffer sb)static TextBufferwrap(CharStream writer)
-
-
-
Constructor Detail
-
Replacer
public Replacer(Pattern pattern, Substitution substitution)
-
Replacer
public Replacer(Pattern pattern, java.lang.String substitution)
-
Replacer
public Replacer(Pattern pattern, java.lang.String substitution, boolean isPerlExpr)
-
-
Method Detail
-
setSubstitution
public void setSubstitution(java.lang.String s, boolean isPerlExpr)
-
replace
public java.lang.String replace(java.lang.String text)
-
replace
public java.lang.String replace(char[] chars, int off, int len)
-
replace
public java.lang.String replace(MatchResult res, int group)
-
replace
public java.lang.String replace(CharStream text, int length) throws IOException
- Throws:
IOException
-
replace
public int replace(java.lang.String text, java.lang.StringBuffer sb)
-
replace
public int replace(char[] chars, int off, int len, java.lang.StringBuffer sb)
-
replace
public int replace(MatchResult res, int group, java.lang.StringBuffer sb)
-
replace
public int replace(MatchResult res, java.lang.String groupName, java.lang.StringBuffer sb)
-
replace
public int replace(CharStream text, int length, java.lang.StringBuffer sb) throws IOException
- Throws:
IOException
-
replace
public int replace(java.lang.String text, TextBuffer dest)
-
replace
public int replace(char[] chars, int off, int len, TextBuffer dest)
-
replace
public int replace(MatchResult res, int group, TextBuffer dest)
-
replace
public int replace(MatchResult res, java.lang.String groupName, TextBuffer dest)
-
replace
public int replace(CharStream text, int length, TextBuffer dest) throws IOException
- Throws:
IOException
-
replace
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.
-
replace
public static int replace(Matcher m, Substitution substitution, CharStream out) throws IOException
- Throws:
IOException
-
replace
public void replace(java.lang.String text, CharStream out) throws IOException- Throws:
IOException
-
replace
public void replace(char[] chars, int off, int len, CharStream out) throws IOException- Throws:
IOException
-
replace
public void replace(MatchResult res, int group, CharStream out) throws IOException
- Throws:
IOException
-
replace
public void replace(MatchResult res, java.lang.String groupName, CharStream out) throws IOException
- Throws:
IOException
-
replace
public void replace(CharStream in, int length, CharStream out) throws IOException
- Throws:
IOException
-
wrap
public static TextBuffer wrap(java.lang.StringBuffer sb)
-
wrap
public static TextBuffer wrap(CharStream writer)
-
-