根据开头字符删除文本文件中的行

时间:2012-02-17 16:16:46

标签: ruby

我的电子邮件信息如下:

Hey how are you?

On Saturday sender@example.com wrote:
> something
> On Friday sender@example.com wrote:
>> previous thing

如何删除以>开头的行以及包含email@example.com wrote的行

我是否应该保留“有人写的”部分,因为这可以删除合法的行,如果它是最后一行,可能只删除该行。

我正在尝试这个:

message_filtered = message_txt.to_s.split("\n").each do |m|
  if m[0] != ">" then
    return m
  end
end

puts message_filtered

我可以将m推送到数组,然后使用\n加入该数组,但我正在尝试更短的方式。

4 个答案:

答案 0 :(得分:4)

尝试

message_filtered = message_txt.lines.reject { |line|
  line[0] == '>' || line =~ YOUR_EMAIL_REGEXP
}.join('\n')

要删除以>开头的行,您可以使用:

message_filtered = message_txt.gsub(/(^>.+)/, '') # should work but not tested

答案 1 :(得分:2)

我的主张:

message_filtered = '';
message_txt.to_s.lines {|line| message_filtered << line unless line[0] == '>' }

答案 2 :(得分:1)

这个怎么样,

> str = "Hey how are you?\nOn Saturday sender@example.com wrote:\n> something\n> On Friday sender@example.com wrote:\n>> previous thing"
> str.split("\n").reject{|msg| msg =~ /^>/ || msg =~ /@example.com/}.join("\n")
 => "Hey how are you?"

答案 3 :(得分:0)

带有简单正则表达式的

String.gsub可以执行此操作:

text = <<EOT
Hey how are you?

On Saturday sender@example.com wrote:
> something
> On Friday sender@example.com wrote:
>> previous thing
EOT

puts text.gsub(/(?:^>|On \w+ sender@example.com wrote:).+\n/m, '')

# => "Hey how are you?\n\n"