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)
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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.