I have String
String test = "
test_one {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
test_two {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
test_three {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
"
I need replace whole text between brackets in 'test_two' or 'test_three' with another text ex "This is my text";
Expected output:
String test = "
test_one {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
test_two {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
test_three {
**This is my text**
}
"
CodePudding user response:
We can do a regex replacement with dot all mode enabled:
String test = "test_one {\nLorem Ipsum is simply dummy text of the printing and typesetting industry.\n}\ntest_two {\nLorem Ipsum is simply dummy text of the printing and typesetting industry.\n}\n\ntest_three {\nLorem Ipsum is simply dummy text of the printing and typesetting industry.\n}";
test = test.replaceAll("(?s)\\btest_three \\{.*?\\}", "test_three {\nThis is my text\n}");
System.out.println(test);
This prints:
test_one {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
test_two {
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
}
test_three {
This is my text
}