Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java String.replaceAll变量_Java - Fatal编程技术网

Java String.replaceAll变量

Java String.replaceAll变量,java,Java,有没有一种快速的方法可以用从匹配的模式中导出的数据替换所有的模式引用 例如,如果我想用相同的数字替换字符串中出现的所有数字,并用0填充固定长度 在这种情况下,如果长度为4,则ab3cd5将变为ab0003cd005 我的想法是使用一个StringBuilder和两个模式:一个模式将获取所有数字,另一个模式将获取所有非数字的内容,并通过找到匹配项的索引将匹配项附加到生成器 我认为可能有更简单的方法。使用appendReplacement和appendTail后,您可能会实现您的目标,如下所示: 导

有没有一种快速的方法可以用从匹配的模式中导出的数据替换所有的模式引用

例如,如果我想用相同的数字替换字符串中出现的所有数字,并用0填充固定长度

在这种情况下,如果长度为4,则
ab3cd5
将变为
ab0003cd005

我的想法是使用一个StringBuilder和两个模式:一个模式将获取所有数字,另一个模式将获取所有非数字的内容,并通过找到匹配项的索引将匹配项附加到生成器


我认为可能有更简单的方法。

使用
appendReplacement
appendTail
后,您可能会实现您的目标,如下所示:

导入java.util.regex.Pattern; 导入java.util.regex.Matcher

String REGEX = "(\\d+)";
String INPUT = "abc3def45";
NumberFormat formatter = new DecimalFormat("0000");

Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // get a matcher object
StringBuffer sb = new StringBuffer();
while(m.find()){
    m.appendReplacement(sb,formatter.format(Integer.parseInt(m.group(1))));
}

m.appendTail(sb);

String result = sb.toString();

使用
appendReplacement
appendTail
后,您可能会达到预期效果,如下所示:

导入java.util.regex.Pattern; 导入java.util.regex.Matcher

String REGEX = "(\\d+)";
String INPUT = "abc3def45";
NumberFormat formatter = new DecimalFormat("0000");

Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // get a matcher object
StringBuffer sb = new StringBuffer();
while(m.find()){
    m.appendReplacement(sb,formatter.format(Integer.parseInt(m.group(1))));
}

m.appendTail(sb);

String result = sb.toString();

如果您确切地知道在任何单个数字之前要填充多少个零,那么类似的方法应该可以工作:

String text = "ab3cd5";
text = text.replaceAll("\\d","0000$0");
System.out.println(text);
否则:

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(text);

StringBuffer result = new StringBuffer();
while(matcher.find()){
    matcher.appendReplacement(result, String.format("%04d", Integer.parseInt(matcher.group()))); 
}
matcher.appendTail(result);
System.out.println(result);

格式
%04d
的意思是:一个整数,由零填充,长度不超过4。

如果您确切知道在任何单个数字之前要填充多少个零,那么类似的方法应该可以工作:

String text = "ab3cd5";
text = text.replaceAll("\\d","0000$0");
System.out.println(text);
否则:

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(text);

StringBuffer result = new StringBuffer();
while(matcher.find()){
    matcher.appendReplacement(result, String.format("%04d", Integer.parseInt(matcher.group()))); 
}
matcher.appendTail(result);
System.out.println(result);

格式
%04d
的意思是:一个整数,由零填充,长度不超过4。

如果你确定数字总是一个位数,那么做正则表达式是非常容易的,但是如果你的填充必须依赖于预先存在的数字的长度,那么我认为没有正则表达式y的方法可以做到这一点(尽管我很好奇其他人怎么说)。如果你确信这个数字总是一个位数,那么做正则表达式是很容易的,但是如果你的填充必须取决于预先存在的数字的长度,那么我不认为有正则表达式y的方法可以做到这一点(尽管我很好奇其他人怎么说)。