Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

書籍 Regular Expressions Cookbook | Remove Duplicate Lines

パンくず

書籍 Regular Expressions Cookbook
Remove Duplicate Lines

概要

重複行の削除について

サンプル

str =<<"EOS"
line1
line1
line2
EOS

puts str
puts "------------------------------------"
print str.gsub(/^(.*)(?:(?:\r?\n|\n)\1)+/,"")

出力

line1
line1
line2
------------------------------------

line2