Java replaceAll()

1 개요[ | ]

Java replaceAll()
Java
Copy
public class MyClass {
    public static void main(String args[]) {
        String str = "135.79.246.80";
        String pattern = "(\\.\\d+)$";
        String replacement = ".x";
        System.out.println( str.replaceAll(pattern, replacement) );
        // 135.79.246.x
    }
}
Java
Copy
public class MyClass {
    public static void main(String args[]) {
        String str = "word <a href=\"word\">word</word>word word";
        System.out.println( str.replaceAll("word(?!([^<]+)?>)", "repl") );
        // repl <a href="word">repl</word>repl repl
    }
}
Java
Copy
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyClass {
    public static void main(String args[]) {
        StringBuffer resultString = new StringBuffer();
        String str = "word <a href=\"word\">word</word>word word";
        String replacement = "repl";
        Pattern p = Pattern.compile("word(?!([^<]+)?>)");
        Matcher m = p.matcher(str);
        while (m.find()) m.appendReplacement(resultString, replacement);
        m.appendTail(resultString);
        System.out.println( resultString.toString() );
        // repl <a href="word">repl</word>repl repl
    }
}

2 #[ | ]

3 같이 보기[ | ]