62,625
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws Exception{
String formData = "1399996aa535bb800";
System.out.println(formData.replaceAll("(\\d{2}).{4}(\\d).{2}(\\d{3}).{2}(\\d).{2}", "$1####$2##$3##$4##"));
//13####6##535##8##
}
@Test
public void t4() {
String str = "1399996aa535bb800";
List<String> ls = new ArrayList<String>();
Pattern pattern = Pattern.compile("(\\w)((?=\\1)(\\1))+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
String temp = matcher.group().replaceAll("\\w", "#");
str = str.replace(matcher.group(), temp);
}
System.out.println(str);
}
我写了个,感觉还可以
/**
*
* @param str 待处理的字符串
* @param replacement 处理成的目标字符
* @param ignoreCase 是否忽略大小写
* @return
*/
public static String filterRepetition(String str,String replacement,boolean ignoreCase) {
String regex="(.)\\1+";
regex=ignoreCase?("(?i)"+regex):regex;
Matcher m=Pattern.compile(regex,Pattern.DOTALL).matcher(str);
StringBuffer result=new StringBuffer();
while(m.find()){
m.appendReplacement(result, m.group().replaceAll(".", replacement));
}
m.appendTail(result);
return result.toString();
}