私はRubyの初心者です。次のようなサンプル(入力テキスト)があります。
Message:
update attributes in file and commit version
----
Modified
「メッセージ」タグの後に行を入れる必要があります。この行は、次のような「メッセージ」で閉じることができることに注意してください
Message:update attributes in file and commit version
私はこのように試しました:
if line =~/Message/
もちろん、次の行は検索しません。
タグ「メッセージ」と「---」の間の行をキャッチする方法を教えてください。いくつかの例を知っている場合は、リンクを入力してください
更新: コード全体
require 'csv'
data = []
File.foreach("new7.txt") do |line|
line.chomp!
if line =~ /Revision/
data.push [line]
elsif line =~ /Author/
if data.last and not data.last[1]
data.last[1] = line
else
data.push [nil, line]
end
elsif line=~/^Message:(.*)^-/m
if data.last and not data.last[2]
data.last[2] = line
else
data.push [nil, nil, line]
end
end
end
CSV.open('new1.csv', 'w') do |csv|
data.each do |record|
csv << record
end
enter code here
入力ファイル:
Revision: 37407
Author: imakarov
Date: 21 июня 2013 г. 10:23:28
Message:my infomation
dmitry name
出力 csv ファイル: