(String中)正则表达式使用如下

package zhengze;
/*
* 正则表达式
*/
public class StringTestZhengZe {
public static void main(String[] args) {
String s = "abghh1efg2hhhlm89llllqr8stull5xyz";
/*
* replaceAll(String regex, String replacement) 用给定的替换替换与给定的 regular
* expression匹配的此字符串的每个子字符串。
*/
// 使用字符串的方法将hh替换成"薇薇"
System.out.println(s.replaceAll("h", "薇薇"));
// 正则表达式:把所有的数字替换成国
System.out.println(s.replaceAll("\d", "微"));
// 将索引的非数字替换成"爱"
System.out.println(s.replaceAll("\D", "维"));

}
}

----------------打印结果-------------------------------

abg薇薇薇薇1efg2薇薇薇薇薇薇lm89llllqr8stull5xyz
abghh微efg微hhhlm微微llllqr微stull微xyz
维维维维维1维维维2维维维维维89维维维维维维8维维维维维5维维维

原文地址:https://www.cnblogs.com/Koma-vv/p/9537333.html