I was wondering if there was a way to remove everything between two carrots from a string:
"<@000000> I thought you said this would happen?"
to get:
"I thought you said this would happen?"
I thought using global substitution would work. I attempted with this code:
somefile = File.open("Test_Teleó.txt", "a+")
jdoc.fetch("messages").each do |body|
ts = body["ts"].to_i
somefile.puts body["text"].gsub(/[<]/, '').gsub(/[>]/, '').chomp
end
And the output is:
@000000 I thought you said this would happen?
It only removed the carrots and not the between characters.
Any advice?
答案 0 :(得分:1)
只需对代码进行微调:
File.open("Test_Teleó.txt", "a+") do |somefile|
jdoc.fetch("messages").each do |body|
ts = body["ts"].to_i
somefile.puts body["text"].gsub(/<[^>]*>/, '').chomp
end
end
我也在这里使用块样式open
,因为它会在块完成时自动关闭文件。这有助于避免忘记和意外打开文件。