Ruby模式匹配和打印错误的行

时间:2016-09-05 20:37:39

标签: ruby parsing

我是Ruby的新手,正在研究如何读取文本文件并检查模式是否匹配。我不确定如何打印错误的线条。

例如,这是文本文件:

id: 1   food: apple, banana
id: 2   food: orange
ids: 3   food: apple, banana
id: 4   food: hello, yellow
id: 5food: apple, banana

阅读文件

File.open(ARGV[0]) do |f1|  
while line = f1.gets  
pattern = /id[:] [[:digit:]]+ food[:] [a-z,]+/
puts line.scan(pattern)
end 

这将打印以下结果

id: 1   food: apple, banana
id: 2   food: orange
id: 4   food: hello, yellow

但我想打错线

ids: 3   food: apple, banana
id: 5food: apple, banana

我不确定如何检查图案是否匹配,然后打印格式不正确的行。

2 个答案:

答案 0 :(得分:1)

如果没有匹配项,

File.open(ARGV[0]) do |f1| while line = f1.gets pattern = /id[:] [[:digit:]]+ synset[:] [a-z,]+/ puts line if line.scan(pattern).empty? end end 将返回一个空数组。所以你可以做到

=~
另一种方式,清洁。您可以使用File.open(ARGV[0]) do |f1| while line = f1.gets pattern = /id[:] [[:digit:]]+ synset[:] [a-z,]+/ puts line unless line =~ pattern end end 方法查看某个行是否与模式匹配。如果模式匹配则返回匹配的索引,如果没有匹配,则返回nil。

targetList.sequence.map(_.suml)

答案 1 :(得分:1)

假设文件被读入变量contents

contents =<<_
id: 1   food: apple, banana
id: 2   food: orange
ids: 3   food: apple, banana
id: 4   food: hello, yellow
id: 5food: apple, banana
_

如果需要food:,您可以使用以下正则表达式。

r = /
    \A                   # match beginning of string
    id:\s+               # match "id:" followed by > 0 spaces
    \d+\s+               # match > 0 digits followed by > 0 spaces
    food:\s+             # match "food:" followed by > 0 spaces
    [[:alpha:]]+         # match > 0 (uppercase or lowercase) letters  
    (?:,\s+[[:alpha:]]+) # match a comma, > 0 spaces, > 0 letters in a non-capture group
    *                    # match > 0 instances of the aforementioned non-capture group
    \n                   # match newline      
    \z                   # match end of string
    /x                   # free-spacing regex definition mode

contents.each_line { |line| puts line if line !~ r }

打印

ids: 3   food: apple, banana
id: 5food: apple, banana