我需要读取一系列行中的文件,然后 根据其中包含 的单词检索特定行。我怎样才能做到这一点?如何仅检索包含特定单词或短语的行?
到目前为止,我看这样行:
lines = File.readlines("myfile.txt")
现在,我需要扫描包含“红”,“兔子”线,“蓝”。我想尽可能在几行代码中完成这部分。
所以,如果我的文件是:
the red queen of hearts.
yet another sentence.
and this has no relevant words.
the blue sky
the white chocolate rabbit.
another irrelevant line.
我想只看到线:
the red queen of hearts.
the blue sky
the white chocolate rabbit.
我想你不想让包含字符串“红”等的行。你想要包含它们的行作为单词。看到我对floatless的答案的评论。 – sawa 2011-05-15 20:30:36
[查找与正则表达式匹配的文本文件中的行]重复(http://stackoverflow.com/questions/6002868/finding-lines-in-a-text-file-matching-a-regular-expression) – Phrogz 2011-05-15 21:33:40