Groovy 字符流模式匹配

发布于 2024-12-06 21:31:27 字数 296 浏览 0 评论 0原文

我有以下代码,从 reader 流中我想删除所有出现的 Command run successed. 请帮我建议任何解决方案

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

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)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2024-12-13 21:31:27

如果您只想要 Command run successed.精确形式,那么您可以这样做:

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

但是,如果您想要更灵活,请忽略空格和大小写来替换它,使用这个:

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

注意,在这两种情况下,因为我们循环遍历每行,所以在换行符中重新添加可能很重要(println打印)。此外,由于在每一行上循环,如果字符串在多行上拆分,则不会替换该字符串。

这是一个完整的示例,您可以运行和修改在 Groovy Web 控制台。 (使用 for...in 链接到原始链接。

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文