本文共 914 字,大约阅读时间需要 3 分钟。
/**
* 将字符串中类似8xxxxxxx转换为8[0-9]{0-7}的形式,而不是8[0-9][0-9][0-9][0-9][0-9][0-9][0-9]这种形式 * @param strInput * @return */ public static String transferRegEx(String strInput) { String str = strInput; //str = str.replaceAll("x.", "[0-9]*");不可这样写8xxxxxxx----->会变成str=8[0-9]*[0-9]*[0-9]*x,应当如下转义 str = str.replaceAll("x\\.", "[0-9]\\*"); int size = strInput.length(); int indexH = 0; int indexT = 0; int count = 0; boolean bFirst = true; while (str.indexOf("x")>=0) { for (int i = 0; i < size; i++) { if (str.charAt(i) == 'x') { if (bFirst) { indexH = i; bFirst = false; } count = count + 1; continue;}
else { if (bFirst == false) { bFirst = true; str = str.substring(0, indexH) + "[0-9]{0-" + count + "}" + str.substring(indexH + count); str = str.trim(); // System.out.println("--------------cishidezifuchuan:" // + str); break; } continue; } } //System.out.println("--------------zuikaojinwhile()de:"+str); } return str; }转载地址:http://txbti.baihongyu.com/