gpt4 book ai didi

ruby - 检查文件是否包含特定单词

转载 作者:数据小太阳 更新时间:2023-10-29 08:19:06 26 4
gpt4 key购买 nike

在 Ruby 中,我如何检查某个文件是否包含某个单词?我试过这个:

foo = File.open('some-file', 'r').read
foo.index(/^word$/)

但这行不通。

最佳答案

File.readlines('some-file').grep(/word/)

请记住,^ 匹配行首,$ 匹配行尾。因此,如果您搜索 ^xyz$,您实际上是在搜索 xyz\n(文件开头)或 \nxyz\n(某处在文件的中间)。没有其他字符串匹配。

关于ruby - 检查文件是否包含特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7081733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com