java正则 替换问题。

chxii 2018-11-15 03:42:15
1399996aa535bb800
13####6##535##8##
上边替换成下边的样子
String tel="13996aa535bb800";
String reg = "(.)\\1";
System.out.println(tel);
Pattern p = Pattern.compile(reg);

Matcher m = p.matcher(tel);

while (m.find()){
String s = m.group();

int len = s.length();
String replace= "";
for(int i=0; i<len; i++){
replace += "#";
}
tel = tel.replaceFirst(s, replace);
}
System.out.println(tel);
这个是我写的。有谁可以看看有什么更简单。更简洁的实现这个功能。java语言
...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
若鱼1919 2018-11-20
  • 打赏
  • 举报
回复

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##
}
随缘心 2018-11-19
  • 打赏
  • 举报
回复
楼上的过滤替换函数在我机器里没跑通,我改造了一下
/**
*
* @param str 待处理的字符串
* @param replacement 处理成的目标字符
* @param ignoreCase 是否忽略大小写
* @return
*/
public static String filterRepetition(String str,char replacement,boolean ignoreCase) {
String regex="(.)\\1+";
regex=ignoreCase?("(?i)"+regex):regex;
Matcher m=Pattern.compile(regex,Pattern.DOTALL).matcher(str);
while(m.find()){
char[] ca = new char[m.group().length()];
Arrays.fill(ca, replacement);
String replaceStr = String.copyValueOf(ca);
str = str.replace(m.group(), replaceStr);
}
return str;
}
htcyrylcmj0415 2018-11-19
  • 打赏
  • 举报
回复


    @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();
	}
咸哼酒家 2018-11-15
  • 打赏
  • 举报
回复
13996aa535bb800
13996aa535bb800
13#96aa535bb800
13##6aa535bb800
13##6aa535bb800
13##6#a535bb800
13##6##535bb800
13##6##535bb800
13##6##535bb800
13##6##535bb800
13##6##535#b800
13##6##535##800
13##6##535##800
13##6##535##8#0
13##6##535##8##
咸哼酒家 2018-11-15
  • 打赏
  • 举报
回复
public static void main(String[] args) {
String tel="13996aa535bb800";
String[] str = tel.split("");
for(int i=0;i<str.length; i++){
if(i+1<str.length && str[i].equals(str[i+1])){
if(0==i){
tel = "#"+tel.substring(1, str.length);
}else{
tel = tel.substring(0, i)+"#"+tel.substring(i+1, str.length);
}

}
if(i>0){
if(str[i-1].equals(str[i])){
if(0==i){
tel = "#"+tel.substring(1, str.length);
}else{
tel = tel.substring(0, i)+"#"+tel.substring(i+1, str.length);
}

}
}

System.out.println(tel);
}
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧