[英]java regexp capture or tabs or spaces on start of string
i try to capture tabs or spaces in the group to use them using replace all it can be or space or tab or both and i like to set it group as i like to use it in the string above (Test_me)我尝试捕获组中的制表符或空格以使用它们全部替换或空格或制表符或两者兼而有之,我喜欢将其设置为组,因为我喜欢在上面的字符串中使用它(Test_me)
This example doesn't work这个例子不起作用
String content = "\t\t<image.name>ABCD:44</docker.image.name>\n";
String content2 = " <image.name>ABCD:44</docker.image.name>\n";
String source = "ABCD:44";
String destination = "${XXXX}";
content = content.replaceAll("(^[ \\s\\t]*)(<image.name>.*" + source + ")(.*?>)",
"$1Test_me\n" +
"$1" + Matcher.quoteReplacement("<image.name>" + destination) + "$3");
If you want to replace whitespace (space, tab etc.) captured by the first capturing group, (\\\\s*)
, you need to omit them (ie $1
) in the second argument.如果要替换第一个捕获组
(\\\\s*)
捕获的空格(空格、制表符等),则需要在第二个参数中省略它们(即$1
)。
Demo:演示:
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
String content = "\t\t<image.name>ABCD:44</docker.image.name>\n";
String content2 = " <image.name>ABCD:44</docker.image.name>\n";
String source = "ABCD:44";
String destination = "${XXXX}";
content = content.replaceAll("(\\s*)(<image.name>.*" + source + ")(.*?>)",
"Test_me\n" + Matcher.quoteReplacement("<image.name>" + destination) + "$3");
System.out.println(content);
}
}
Output:输出:
Test_me
<image.name>${XXXX}</docker.image.name>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.