使用Ruby插入文本

时间:2016-08-03 03:17:16

标签: ruby text text-files text-manipulation

我有一个.txt文件,其文字布局如下:

11/14/2015, 13:51: John Doe: Hi there
11/14/2015, 13:52: Jane Doe: Hi, my name is Jane.
Nice to meet you.
11/14/2015, 13:53: Joe Bloggs: Hey there everyone!
Shall we get started?

我想要完成的是在缺少标签的消息前面插入前一条消息的“标签”。例如,最终结果如下所示:

11/14/2015, 13:51: John Doe: Hi there
11/14/2015, 13:52: Jane Doe: Hi, my name is Jane.
11/14/2015, 13:52: Jane Doe: Nice to meet you.
11/14/2015, 13:53: Joe Bloggs: Hey there everyone!
11/14/2015, 13:53: Joe Bloggs: Shall we get started?

我将如何做到这一点?

2 个答案:

答案 0 :(得分:1)

txt =<<_
11/14/2015, 13:51: John Doe: Hi there
11/14/2015, 13:52: Jane Doe: Hi, my name is Jane.
Nice to meet you.
11/14/2015, 13:53: Joe Bloggs: Hey there everyone!
Shall we get started?
_

R = %r{\A\d{2}/\d{2}/\d{4}\,\s\d{2}:\d{2}:\s}

arr = txt.split("\n")
  #=> ["11/14/2015, 13:51: John Doe: Hi there",
  #    "11/14/2015, 13:52: Jane Doe: Hi, my name is Jane.",
  #    "Nice to meet you.",
  #    "11/14/2015, 13:53: Joe Bloggs: Hey there everyone!",
  #    "Shall we get started?"] 
(1..arr.size-1).each do |i|
  next if arr[i] =~ R
  previous_line = arr[i-1]
  leader = previous_line[0, 2 + previous_line.rindex(": ")]
  arr[i] = leader.concat(arr[i]) 
end
arr
  #=> ["11/14/2015, 13:51: John Doe: Hi there",
  #    "11/14/2015, 13:52: Jane Doe: Hi, my name is Jane.",
  #    "11/14/2015, 13:52: Jane Doe: Nice to meet you.",
  #    "11/14/2015, 13:53: Joe Bloggs: Hey there everyone!",
  #    "11/14/2015, 13:53: Joe Bloggs: Shall we get started?"] 

答案 1 :(得分:1)

input = StringIO.new <<~_
  11/14/2015, 13:51: John Doe: Hi there
  11/14/2015, 13:52: Jane Doe: Hi, my name is Jane.
  Nice to meet you.
  11/14/2015, 13:53: Joe Bloggs: Hey there everyone!
  Shall we get started?
_

label = nil
output = input.each_with_object("") do
  |l, s|
  if l =~ %r[\A\d{2}/\d{2}/\d{4}, \d{2}:\d{2}: ]
    label = $&
    s.concat(l)
  else
    s.concat(label + l)
  end
end
puts output

输出

11/14/2015, 13:51: John Doe: Hi there
11/14/2015, 13:52: Jane Doe: Hi, my name is Jane.
11/14/2015, 13:52: Nice to meet you.
11/14/2015, 13:53: Joe Bloggs: Hey there everyone!
11/14/2015, 13:53: Shall we get started?