日期:2014-05-20 浏览次数:20905 次
public static void main(String[] args) { String s ="abc11,cd123 f7895"; String s2 = s.replaceAll("(?<!f)[0-9]+?", "*"); System.out.println(s); System.out.println(s2); }
String s2 = s.replaceAll("(?<!e)[0-9]+?", "*");
------解决方案--------------------
分开来吧,一次性的正则不容易
------解决方案--------------------
这样吧
String s ="abc11,cd123 f7895"; String[] s2 = s.split("(?<!\\w)"); StringBuffer sb = new StringBuffer(); for(int i = 1; i < s2.length;i++) { if(s2[i].charAt(0)!='f') { sb.append(s2[i].replaceAll("\\d","*")); } else { sb.append(s2[i]); } } System.out.println(s); System.out.println(sb);
------解决方案--------------------
f\d+
------解决方案--------------------
String s = "abc11,cd123 f7895 abcd1234 ef1234 abc123"; Pattern p = Pattern.compile(".*?\\d+"); Matcher m = p.matcher(s); StringBuffer sb = new StringBuffer(); while (m.find()) { if (!m.group().matches("\\W*f\\d+")) { // 如果是f后跟数字不替换就用 .*?f\\d+ m.appendReplacement(sb, m.group().replaceAll("\\d+", "")); } } m.appendTail(sb); System.out.println(sb);
------解决方案--------------------
String str="abc11,cd123 f7895"; str=" "+str+" "; Pattern pat=Pattern.compile("[^f0-9][0-9]+(?=[^0-9])"); Matcher mcr=pat.matcher(str); StringBuffer sb=new StringBuffer(); while(mcr.find()){ mcr.appendReplacement(sb, mcr.group().replaceAll("[0-9]", "*")); } mcr.appendTail(sb); str=sb.toString().replaceAll("^ | $",""); System.out.println(str);
------解决方案--------------------
就看LZ什么需求了
String s = "abc11,cd123 f7895 abcd1234 ef1234 abc123"; s = s.replaceAll("([^f\\d]+)\\d+", "$1"); System.out.println(s);