Wednesday, 8 February 2017

Regex : appendReplacement / appendTail






public class AppendTailReplacement {

    public static void main(String[] args) {

        String text    =
                "John writes about this, and John Doe writes about that," +
                        " and John Wayne writes about everything."                ;
        String patternString1 = "((John) (.+?)) ";
        Pattern pattern      = Pattern.compile(patternString1);        Matcher matcher      = pattern.matcher(text);        StringBuffer stringBuffer = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(stringBuffer, "Joe Blocks ");            System.out.println(stringBuffer.toString());        }
        matcher.appendTail(stringBuffer);
        System.out.println(stringBuffer.toString());    }
}

No comments:

Post a Comment