正则替换

/**
* 数字替换,适当位置加-
*/
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


posted @ 2018-05-10 17:14  慕橙-abby  阅读(480)  评论(0编辑  收藏  举报