Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
"and i know that i cannot use named groups with this version of Java" Numbered groups are OK, though :) –  dasblinkenlight Apr 17 '13 at 10:40

2 Answers 2

up vote 1 down vote accepted

try

    s = s.replaceAll("\\$\\{(.+?)}", "$1");
share|improve this answer
    
Dasblinkenlight's solution is perfectly valid as well, so thank you both! –  ilPittiz Apr 17 '13 at 14:11

You can use capturing groups in the replacement string, like this:

str.replaceAll("[$][{](.*?)[}]", "$1");

Link to a demo on ideone.

Note that the character group [${] matches either a $ by itself or a { by itself, so you should rewrite it to match ${ together.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.