Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String: Replace a string containing $ sign

Tags:

java

regex

How can I replace a String $1 in Java? I tried this, but this doesn't replace it:

System.out.println(someHTMLCodeAsString.replaceAll("$1", "REPLACED"));
like image 988
Poru Avatar asked Jun 14 '26 14:06

Poru


1 Answers

The $ is being interpreted as regex instead of as a character (it means 'end of line'). Try System.out.println(someHTMLCodeAsString.replaceAll("\\$1", "REPLACED"));

like image 75
Chris Dennett Avatar answered Jun 17 '26 06:06

Chris Dennett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!