Search code examples
groovygroovyshellgroovy-console

Syntax error in removing bad character in groovy


Hello I have a string like a= " $ 2 187.00" . I tried removing all the white spaces and the bad characters like a.replaceAll("\\s","").replace("$","") . but i am getting error Impossible to parse JSON response: SyntaxError: JSON.parse: bad escaped character how to remove the bad character in this expression so that the value becomes 2187.00.Kindly help me .Thanks in advance


Solution

  • def a = ' $ 2 187.00'
    a.replaceAll(/\s/,"").replaceAll(/\$/,"")
    
    // or simply
    a.replaceAll(/[\s\$]/,"")
    

    It should return 2187.00.

    Note

    1. that $ has special meaning in double quoted strings literals "" , called as GString.
    2. In groovy, you can user regex literal, using that is better than using regex with multiple escape sequences in string.