package cn.netkiller;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("www|netkiller");
Matcher matcher = pattern.matcher("https://www.netkiller.cn/linux/index.html");
if (matcher.find()) {
String s = matcher.replaceFirst("api"); //替换后的字符串
System.out.println(s);
}
}
}
package cn.netkiller;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("www|netkiller");
Matcher matcher = pattern.matcher("https://www.netkiller.cn/linux/index.html");
if (matcher.find()) {
String s = matcher.replaceAll("test"); //替换后的字符串
System.out.println(s);
// 输出结果:https://test.test.cn/linux/index.html
}
}
}
"aab".replaceAll("a{1}", "x"); //xxb
"aba".replaceAll("a{1}", "x"); //xbx
"abaaabaaaba".replaceAll("a{2}", "x"); //abxabxaba
"abaabaaaaba".replaceAll("a{2}", "x"); //abxbxxba