日期:2014-05-18 浏览次数:20640 次
//TEST String[] telNo = new String[]{ "13421159889","13421159888","13421158888","13421158734"}; for(int i=0;i<telNo.length;i++) System.out.println(matchType(telNo[i])); /** * 6、8、9的情况,跟升降序的情况没有考虑. * @param telNo * @return */ public static String matchType(String telNo){ String rtn = telNo; String[] match = {"ABBA","AABB","ABBB","AAAB","AABA","ABAA","AAAA","AAA","ABCD"}; Arrays.sort(match); List list = Arrays.asList(match); int len = 0;//替换位当前位置 int loop = 1;//开始替换位 int step = 0; String compare = ""; String pat = "ABCD"; while(loop<=11){ if(compare.indexOf(telNo.substring(len,len+1)) == -1){ rtn = rtn.replaceAll(telNo.substring(len,len+1), pat.substring(step,step+1)); step++; compare += telNo.substring(len,len+1); } len++; //替换位置下移一位. if(len>=11 || step>=4){ //查找匹配位置. Matcher m = Pattern.compile("A[ABCD]+$").matcher(rtn); if(m.find() && list.indexOf(m.group(0)) != -1){ return m.group(0); } //还原 len = loop++; step = 0; compare = ""; rtn = telNo; } } return rtn; }