1234
1234

Reputation: 241

Groovy remove part of string within < >

Hi In Groovy I need to remove part of string the string.

 <Results xsi:type="xsd:string" 

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Updated User 

            id:nish.test11</Results>  

should look like " Updated User id:nish.test11

how can i do that?

Upvotes: 0

Views: 322

Answers (2)

injecteer
injecteer

Reputation: 20699

If I run the code:

""" <Results xsi:type="xsd:string" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Updated User 

        id:nish.test11</Results>""".replaceAll( /<\/?[^<>]>/, '' ).replaceAll( /[\n\s]+/, ' ' )

it gives me

Updated User id:nish.test11

as output

Upvotes: 0

kdabir
kdabir

Reputation: 9868

As the content looks like an XML,

def xml = """
 <Results xsi:type="xsd:string" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Updated User 
    id:nish.test11</Results>  
"""

it's better to use XmlSlurper than parsing/extracting strings by hand

def result = new XmlSlurper().parseText(xml)            

println result.toString()

this gives the desired result (the content of the Result)

Upvotes: 1

Related Questions