常用正则(持续更新)
2018-09-11 本文已影响0人
heavyRain
记录自己平常写的一些正则,有问题欢迎指出
/**
* 如text:123456,seat:**, return 12**56
* 需要注意 text和seat的长度,text长度<seat长度加2会直接返回text
* @param text 原字符串
* @param seat 需要替换的字符 如:****
* @return 替换后的字符串
*/
public static String hideString(String text, String seat) {
String hidePhone;
int length = text.length();
if (length < seat.length() + 2) {
hidePhone = text;
} else {
int leftCount = (length - seat.length()) / 2;
int rightCount = length - seat.length() - leftCount;
String regex = "(.{" + leftCount + "}).{" + seat.length() + "}(.{" + rightCount + "})";
hidePhone = text.replaceAll(regex, "$1" + seat + "$2");
}
return hidePhone;
}