我有以下代码,从reader流中,我想删除所有出现的Command ran successfully.请帮助我建议任何解决方案
String ostream="license_all (47286) Command ran successfully. License a Command ran successfully."
Reader reader = new StringReader(ostream)
for(c in reader)
{
print(c)
}发布于 2011-09-28 15:23:13
如果您只需要Command ran successfully.的确切形式,那么您可以这样做:
reader.eachLine{ c ->
println c.replaceAll('Command ran successfully.', '')
}但是,如果您希望更灵活,并在不考虑空格和大小写的情况下替换它,请使用以下命令:
reader.eachLine{ c ->
println c.replaceAll(/(?i)command\s+ran\s+successfully\s*\./, '')
}请注意,在这两种情况下,由于我们循环遍历每一行,因此在换行符(println与print)中添加回行符可能很重要。此外,由于在每一行上进行循环,如果字符串拆分在多行上,这将不会替换字符串。
下面是您可以运行和修改on the Groovy Web Console的a complete example。(Link to original one with for...in.)
https://stackoverflow.com/questions/7579094
复制相似问题