我输入字符串为
input = "AAA10.50.30.20"
input.replaceAll("10.([01]?\\d\\d?|2[0-4]\\d|25[0-5]).([01]?\\d\\d?|2[0-4]\\d|25[0-5]).([01]?\\d\\d?|2[0-4]\\d|25[0-5])",
"X");
输出为:"AAAX"
但我希望输出为"AAAXXXXXXXXXXX"
它应该用多个'X'
替换IP,这等于IP地址中的字符数
答案 0 :(得分:1)
如果您只想通过X
致电str.replaceAll("\\d|\\.", "X")
替换所有数字和点数。如果你想匹配确切的模式,请使用类似
str.replaceAll("(?:\\d{1,3}\\.){3}\\d{1,3}", "X")
答案 1 :(得分:1)
String input = "AAA10.50.30.20";
Pattern p = p = Pattern.compile("10.([01]?\\d\\d?|2[0-4]\\d|25[0-5]).([01]?\\d\\d?|2[0-4]\\d|25[0-5]).([01]?\\d\\d?|2[0-4]\\d|25[0-5])");
Matcher m = p.matcher(input);
String result = input;
while(m.find()){
char[] replacement = new char[m.end()-m.start()];
Arrays.fill(replacement, 'X');
result = result.substring(0, m.start())
+ new String(replacement)
+ result.substring(m.end());
}
return result;
答案 2 :(得分:0)
问题是您使用正则表达式搜索IP地址。这给你一个匹配 - IP地址。然后用X替换它,给你AAAX。如何获取正则表达式匹配的字符串,然后用X替换所有数字?