-1

I am trying to replace a particular word say password to ******* from a string which has characters such as $ and \n in Groovy.

I cannot escape them by using \ because i have no control over data i receive and even in the final output i need as it was earlier with $.

i tried str.replaceAll("password","**")

gives:

illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 2, column 8. afdmas$

def str="""hello how  
you$
password
doing"""

expected o/p :

hello how  
you$
**
doing
cfrick
  • 35,203
  • 6
  • 56
  • 68
Rohan
  • 3
  • 2
  • You can try to use an instance of a `String` with `''` instead of `GString` which is quoted with `""`. – Opal May 13 '19 at 20:00

1 Answers1

1

It's not clear why you don't have access to the input data because you use string literal in the example and the error you get is from this example as well. In this case you can use Groovy single-quoted strings but they are without interpolation. Or if interpolation is necessary then you can use slashy or dollar slashy strings which additionally don't require backslash escaping:

def str='''"hello how
you$ password doing'''
def str=/"hello how
you$ password doing/
def str=$/"hello how
you$ password doing/$

*Formatting is the same as in OP

Dmitry Khamitov
  • 3,061
  • 13
  • 21