正则替换
/**
* 数字替换,适当位置加-
*/
public static void main(String[] args) {
String regex = "^(\\d{2})(\\d)(\\d{4})(\\d{4})(\\d)$";
String test = "121123412344";
System.out.println("是否匹配(true/false):" + Pattern.compile(regex).matcher(test).matches());
System.out.println(test.replaceAll(regex, "$1-$2-$3-$4-$5"));
}
输出:
是否匹配(true/false):true
12-1-1234-1234-4