Forum Discussion

shamane2000's avatar
shamane2000
Occasional Contributor
7 years ago
Solved

Groovy operations trim() and replace() seems to doesn't work in Soapui

I tried to get rid of whitespaces and newlines with groovy.

But if I try this in Groovy, the resul is either a not trimed string, or an error (in the case of "replace()" )

 

 

String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"
log.info("before trim: " + response)
log.info("After replace1: " + response.trim() )
Output is:
After replace1: <blub>
Blaaaah asd
<Blä          />          asd       asdsad
  asdasd      asdasd</blub>
*/
and with "replace" it also not work:
log.info("After replace2: " + response.replace("[\n\r]*", "" )
and there just came a Error message:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script81.groovy: 11: unexpected token: @ line 11, column 63. sponse.replace("[\n\r]*", "" ) ^ org.codehaus.groovy.syntax.SyntaxException: unexpected token: @ line 11, column 63. at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:143) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:111) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:237) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:167) at org.codehaus.groovy.control.

....

*/

 

Whats wrong here?

 

PS: I am using ReadyAPI 2.0.2

PPS: yes, I have also tried to do it with xpath "normalize-space()", but there I have another problem - the XML-Structure is lost... maybe I have to post another question about that...

  • Is this your actual response?

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"

     

    Or is should look like this?

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä/>asdasdsad \r\n  asdasdasdasd</blub>"

     

    Please confirm on this.. And both replace() and  replaceAll() method works.. Trying putting correct regular expressions. See below code.

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"
    log.info("before trim: " + response)
    log.info("After replace1: " + response.trim())
    log.info("After replace2: " + response.replaceAll("(\r\n)", ""))

     

    This is how output look like :-

     

    • Mon Feb 19 22:19:57 IST 2018:INFO:before trim: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:19:57 IST 2018:INFO:After replace1: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:19:57 IST 2018:INFO:After replace2: Blaaaah asd<Blä /> asd asdsad asdasd asdasd

     

    With replace() method:-

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"
    log.info("before trim: " + response)
    log.info("After replace1: " + response.trim())
    log.info("After replace2: " + response.replace("(\r\n)", ""))

    Output:-

     

    • Mon Feb 19 22:26:21 IST 2018:INFO:before trim: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:26:21 IST 2018:INFO:After replace1: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:26:21 IST 2018:INFO:After replace2: Blaaaah asd <Blä /> asd asdsad asdasd asdasd

     

    And trim() in the sense you mean to remove the white spaces inside the String :-

     

    Like this:- <blub>Blaaaahasd<Blä/>asdasdsadasdasdasdasd</blub> ..

     

    To do this you need to use replaceAll() method like this:-

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"
    log.info("After replace1: " + response.replaceAll("\\s+",""))

    Output look like this:-

     

    Mon Feb 19 22:33:48 IST 2018:INFO:After replace1: <blub>Blaaaahasd<Blä/>asdasdsadasdasdasdasd</blub>

     

    Hope, it resolve your problem. :)

7 Replies

  • avidCoder's avatar
    avidCoder
    Super Contributor

    Is this your actual response?

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"

     

    Or is should look like this?

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä/>asdasdsad \r\n  asdasdasdasd</blub>"

     

    Please confirm on this.. And both replace() and  replaceAll() method works.. Trying putting correct regular expressions. See below code.

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"
    log.info("before trim: " + response)
    log.info("After replace1: " + response.trim())
    log.info("After replace2: " + response.replaceAll("(\r\n)", ""))

     

    This is how output look like :-

     

    • Mon Feb 19 22:19:57 IST 2018:INFO:before trim: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:19:57 IST 2018:INFO:After replace1: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:19:57 IST 2018:INFO:After replace2: Blaaaah asd<Blä /> asd asdsad asdasd asdasd

     

    With replace() method:-

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"
    log.info("before trim: " + response)
    log.info("After replace1: " + response.trim())
    log.info("After replace2: " + response.replace("(\r\n)", ""))

    Output:-

     

    • Mon Feb 19 22:26:21 IST 2018:INFO:before trim: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:26:21 IST 2018:INFO:After replace1: Blaaaah asd <Blä /> asd asdsad asdasd asdasd
    • Mon Feb 19 22:26:21 IST 2018:INFO:After replace2: Blaaaah asd <Blä /> asd asdsad asdasd asdasd

     

    And trim() in the sense you mean to remove the white spaces inside the String :-

     

    Like this:- <blub>Blaaaahasd<Blä/>asdasdsadasdasdasdasd</blub> ..

     

    To do this you need to use replaceAll() method like this:-

     

    String  response = " <blub>\r\nBlaaaah asd\r\n<Blä          />          asd       asdsad \r\n  asdasd      asdasd</blub>"
    log.info("After replace1: " + response.replaceAll("\\s+",""))

    Output look like this:-

     

    Mon Feb 19 22:33:48 IST 2018:INFO:After replace1: <blub>Blaaaahasd<Blä/>asdasdsadasdasdasdasd</blub>

     

    Hope, it resolve your problem. :)

    • shamane2000's avatar
      shamane2000
      Occasional Contributor

      Hi ashutoshanshu,

       

      thanks a lot, your syntax works. I realy don't know why I haven't found this solution, maybe it was to late for me yesterday... ;-)

       

      Do you have also a clue, how to do this in a xpath or xqerry?

      If I try this like this:

      replace(/*,"\\s+","")

      then my XML structure is away. (in a Property Transfer Step)

       

      input:
      
      <node>test1<subnode>test2</subnode>test3</node>
      
      output:
      
      test1 test2 test3

       

       

      best regards,

      Shamane2000

      • avidCoder's avatar
        avidCoder
        Super Contributor

        You should know that replace() or replaceAll() method takes only 2 arguments within quotes like this :- replace("", "") This is not the correct way of using this method :- replace(/*,"\\s+", "") and by the way, what is your input String and what is your expected output?