[英]Ruby pattern matching and printing the wrong line
我是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
我不確定如何檢查模式是否不匹配,然后打印格式錯誤的行。
如果沒有匹配項, scan
將返回一個空數組。 所以你可以做
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
換句話說,更清潔。 您可以使用=~
方法查看行是否與模式匹配。 如果模式匹配,則返回匹配索引;如果不匹配,則返回nil。
File.open(ARGV[0]) do |f1|
while line = f1.gets
pattern = /id[:] [[:digit:]]+ synset[:] [a-z,]+/
puts line unless line =~ pattern
end
end
假設文件被讀入變量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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.