Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a String starting with $ character using Java's String.replaceAll method

Tags:

java

string

regex

What's the correct regex for a String starting with $ char as the first argument (i.e. the string to replace) to Java's replaceAll method in the String class? I can't get the syntax right. Example

String s = "SUMIF($C$6:$C$475,\"   India - Hyd\",K$6:K$475)";
System.out.println(s.replaceAll("$475", "$44"));
like image 450
user3580890 Avatar asked Sep 17 '26 01:09

user3580890


2 Answers

I would suggest in this case you use replace() instead of replaceAll(), because you're not using any regex:

System.out.println(s.replace("$475", "$44"));
like image 84
shmosel Avatar answered Sep 19 '26 13:09

shmosel


This should work:

System.out.println(s.replaceAll("\\$475", "\\$44"));
like image 45
Jens Avatar answered Sep 19 '26 14:09

Jens