Saturday, May 22, 2010

Learning Basic Parsing 3

package com.andia.aryandie.latihan.parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Testing {
    private static String REGEX = "a*b";

    private static String INPUT = "aabfooaabfooabfoob";

    private static String REPLACE = "-";

    public static void main(String[] args) {
        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(INPUT); // get a matcher object
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, REPLACE);
        }
        m.appendTail(sb);
        System.out.println(sb.toString());
    }
}

Output 

-foo-foo-foo-

No comments: