package com.zoro.test;
/**
* (.+(?=\()) 获取 ( 前的字符串
*
* @author yx.Jiang
* @desc
* @date 2022-04-28 21:54
*/
public class ReplaceStr {
public static void main(String[] args) {
String tel = "+86(10)5730-4332";
String format = format(tel);
System.out.println("format = " + format);
}
/**
* 格式化固话号码
*
* @param tel 固话 +86(10)5730-4332
* @return 格式化后的电话号码
*/
private static String format(String tel) {
//判空 isNotBlank(tel)
//获取省号
String provinceNo = "";
if (tel.contains("(") && tel.contains(")")) {
//10
provinceNo = tel.substring(tel.indexOf("(")+1, tel.indexOf(")"));
}
//去除国号及省号 +86(10)
if (tel.contains("+") && tel.contains(")")) {
//5730-4332
tel = tel.replaceAll("(\\+)(\\d|\\()*(\\))", "");
}
//省号长度为2补0
if (provinceNo.length() == 2) {
//010
provinceNo = "0" + provinceNo;
}
return String.format("%s-%s",provinceNo, tel);
}
}