Groovy删除<中的部分字符串>

时间:2014-02-18 04:26:37

标签: string groovy

您好在Groovy我需要删除部分字符串 字符串。

 <Results xsi:type="xsd:string" 

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

            id:nish.test11</Results>  

应该看起来像“更新用户ID:nish.test11

我该怎么做?

2 个答案:

答案 0 :(得分:1)

由于内容看起来像XML,

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

最好使用XmlSlurper而不是手工解析/提取字符串

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

println result.toString()

这给出了期望的结果(Result

的内容

答案 1 :(得分:0)

如果我运行代码:

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

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

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

它给了我

Updated User id:nish.test11

作为输出

相关问题