ilPittiz
ilPittiz

Reputation: 754

Java regex - replace substrings between delimiters with same substrings without delimiters

I'm trying to figure out how to replace with Java 1.6 in strings like

hello ${world }!   ${txt + '_t'}<br/> ${do_not_replace

any substring identified between '${' and '}' with the same substring without these delimiters. So the output for the string above should be

hello world !   txt + '_t'<br/> ${do_not_replace

I identified a working pattern that allows me to replace the substrings with a fixed string

str.replaceAll('[${](.*?)}', '_')

and i know that i cannot use named groups with this version of Java.

Any suggestion for a simple solution to this problem are highly appreciated! Many thanks

Upvotes: 1

Views: 882

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136052

try

    s = s.replaceAll("\\$\\{(.+?)}", "$1");

Upvotes: 2

Related Questions