Class Replacer


  • public class Replacer
    extends java.lang.Object
    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, PerlSubstitution, Replacer(totalcross.util.regex.Pattern,totalcross.util.regex.Substitution)
    • Constructor Detail

      • Replacer

        public Replacer​(Pattern pattern,
                        java.lang.String substitution)
      • Replacer

        public Replacer​(Pattern pattern,
                        java.lang.String substitution,
                        boolean isPerlExpr)