Example usage for java.util.regex Matcher replaceAll

List of usage examples for java.util.regex Matcher replaceAll

Introduction

In this page you can find the example usage for java.util.regex Matcher replaceAll.

Prototype

public String replaceAll(Function<MatchResult, String> replacer) 

Source Link

Document

Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

Usage

From source file:Main.java

public static void main(String[] args) {
    String word = "77933211";
    Pattern pattern = Pattern.compile("(\\d)\\1");
    Matcher matcher = pattern.matcher(word);
    word = matcher.replaceAll("$1");
    System.out.println(word);/*  w  ww .  j a v  a 2  s .co  m*/
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(i|I)ce");
    String candidateString = "ice Ice Ice.";

    Matcher matcher = p.matcher(candidateString);
    String tmp = matcher.replaceAll("Java");

    System.out.println(tmp);//  w w  w.j a v a 2 s  . c om
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(i|I)ce");

    String candidateString = "Ice. ice Ice Ice.";

    Matcher matcher = p.matcher(candidateString);
    String tmp = matcher.replaceAll("Java");

    System.out.println(tmp);/*from w  w  w  . jav  a 2  s .  c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String patternStr = "\\((\\w+)\\)";
    String replaceStr = "<$1>";
    Pattern pattern = Pattern.compile(patternStr);

    CharSequence inputStr = "a (b c) d (ef) g";
    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replaceStr);

}

From source file:ReplaceExample.java

public static void main(String args[]) {
    String regex = "(\\w)(\\d)(\\w+)";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "X99SuperJava";
    Matcher matcher = pattern.matcher(candidate);
    String tmp = matcher.replaceAll("$33");

    System.out.println("REPLACEMENT: " + tmp);
    System.out.println("ORIGINAL: " + candidate);
}

From source file:Main.java

public static void main(String[] args) {
    CharSequence inputStr = "q w e r t ";
    String patternStr = "q";
    String replacementStr = "s";
    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
    System.out.println(output);//w ww  . j  av a 2s.c om
}

From source file:Main.java

public static void main(String[] args) {
    CharSequence inputStr = "quit";
    String patternStr = "q";
    String replacementStr = "s";

    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
}

From source file:Main.java

public static void main(String[] args) {
    String source = "The quick brown fox jumps over the brown lazy dog.";
    String find = "brown";
    String replace = "red";

    Pattern pattern = Pattern.compile(find);
    Matcher matcher = pattern.matcher(source);
    String output = matcher.replaceAll(replace);

    System.out.println("Source = " + source);
    System.out.println("Output = " + output);
}

From source file:Main.java

public static void main(String[] args) {
    String s = "stuff1 (foo1(bar1)foo2) stuff2 (bar2) stuff3";
    String re = "\\([^()]*\\)";
    Pattern p = Pattern.compile(re);
    Matcher m = p.matcher(s);
    while (m.find()) {
        s = m.replaceAll("");
        m = p.matcher(s);//from   w  ww. jav a  2  s.c om
    }
    System.out.println(s);
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b(\\d{3})(\\d{3})(\\d{4})\\b";
    String replacementText = "($1) $2-$3";
    String source = "1234567890, 12345, and 9876543210";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(source);

    String formattedSource = m.replaceAll(replacementText);

    System.out.println("Text: " + source);
    System.out.println("Formatted Text: " + formattedSource);
}