Wednesday, 8 February 2017

Regex : replaceAll / replaceFirst





public class ReplaceAllFirst {

    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);
        String replaceAll = matcher.replaceAll("Joe Blocks ");        System.out.println("replaceAll   = " + replaceAll);
        String replaceFirst = matcher.replaceFirst("Joe Blocks ");        System.out.println("replaceFirst = " + replaceFirst);    }

}

No comments:

Post a Comment