jregex
Class Replacer

java.lang.Object
  |
  +--jregex.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(jregex.Pattern,jregex.Substitution)

Constructor Summary
Replacer(Pattern pattern, java.lang.String substitution)
           
Replacer(Pattern pattern, java.lang.String substitution, boolean isPerlExpr)
           
Replacer(Pattern pattern, Substitution substitution)
           
 
Method Summary
 java.lang.String replace(char[] chars, int off, int len)
           
 int replace(char[] chars, int off, int len, java.lang.StringBuffer sb)
           
 int replace(char[] chars, int off, int len, TextBuffer dest)
           
 void replace(char[] chars, int off, int len, java.io.Writer out)
           
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.
static int replace(Matcher m, Substitution substitution, java.io.Writer out)
           
 java.lang.String replace(MatchResult res, int group)
           
 int replace(MatchResult res, int group, java.lang.StringBuffer sb)
           
 int replace(MatchResult res, int group, TextBuffer dest)
           
 void replace(MatchResult res, int group, java.io.Writer out)
           
 int replace(MatchResult res, java.lang.String groupName, java.lang.StringBuffer sb)
           
 int replace(MatchResult res, java.lang.String groupName, TextBuffer dest)
           
 void replace(MatchResult res, java.lang.String groupName, java.io.Writer out)
           
 java.lang.String replace(java.io.Reader text, int length)
           
 int replace(java.io.Reader text, int length, java.lang.StringBuffer sb)
           
 int replace(java.io.Reader text, int length, TextBuffer dest)
           
 void replace(java.io.Reader in, int length, java.io.Writer out)
           
 java.lang.String replace(java.lang.String text)
           
 int replace(java.lang.String text, java.lang.StringBuffer sb)
           
 int replace(java.lang.String text, TextBuffer dest)
           
 void replace(java.lang.String text, java.io.Writer out)
           
 void setSubstitution(java.lang.String s, boolean isPerlExpr)
           
static TextBuffer wrap(java.lang.StringBuffer sb)
           
static TextBuffer wrap(java.io.Writer writer)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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(java.io.Reader text,
                                int length)
                         throws java.io.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(java.io.Reader text,
                   int length,
                   java.lang.StringBuffer sb)
            throws java.io.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(java.io.Reader text,
                   int length,
                   TextBuffer dest)
            throws java.io.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,
                          java.io.Writer out)
                   throws java.io.IOException

replace

public void replace(java.lang.String text,
                    java.io.Writer out)
             throws java.io.IOException

replace

public void replace(char[] chars,
                    int off,
                    int len,
                    java.io.Writer out)
             throws java.io.IOException

replace

public void replace(MatchResult res,
                    int group,
                    java.io.Writer out)
             throws java.io.IOException

replace

public void replace(MatchResult res,
                    java.lang.String groupName,
                    java.io.Writer out)
             throws java.io.IOException

replace

public void replace(java.io.Reader in,
                    int length,
                    java.io.Writer out)
             throws java.io.IOException

wrap

public static TextBuffer wrap(java.lang.StringBuffer sb)

wrap

public static TextBuffer wrap(java.io.Writer writer)