regex - most elegant way of escaping $ in java -


i have base string "abc def", trying replace base string "abc$ def$" using replacefirst(), running errors $ not escaped.

i tried doing pattern , matcher apis, given below,

newvalue = "abc$ def$";  if(newvalue.contains("$")){     pattern specialcharacters = pattern.compile("$");     matcher newmatchervalue = specialcharacters.matcher(newvalue) ;     newvalue = newmatchervalue.replaceall("\\\\$") ; } 

this runs error. there elegant way of replacing second string "abc$ def$" "abc\\\\$ def\\\\$" use replacefirst() api successfully?

look @ pattern.quote() quote regex , matcher.quotereplacement() quote replacement string.

that said, want to?

system.out.println("abc def".replaceall("([\\w]+)\\b", "$1\\$")); 

this prints out abc$ def$


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -