开发者

Groovy char stream pattern matching

I have the following开发者_高级运维 code, from the reader stream i want to remove all occurances of Command ran successfully. Please help me suggest any solution

String ostream="license_all (47286) Command ran successfully. License a Command ran     successfully."
Reader reader = new StringReader(ostream)
for(c in reader)
{
print(c)
}


If you only want the exact form of Command ran successfully., then you can do this:

reader.eachLine{ c ->
    println c.replaceAll('Command ran successfully.', '')
}

However, if you want to be more flexible, and replace it disregarding spaces and case, use this:

reader.eachLine{ c ->
    println c.replaceAll(/(?i)command\s+ran\s+successfully\s*\./, '')
}

Note, that in both instances, because we are looping over each line, it may be important to add back in the line break (println vs print). Also, due looping over each line, this will not replace the string if it occurs split on more than one line.

Here's a complete example which you can run and modify on the Groovy Web Console. (Link to original one with for...in.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜