[英]Remove empty lines from a string in ruby
我經歷過其他類似的問題,他們似乎沒有解釋我的問題。
我的輸出,就是這樣,我想從ruby中的字符串中刪除空行,
#
CIRRUS LADIES NIGHT with DJ ROHIT
4th of JULY Party ft. DJ JASMEET @ I-Bar
Submerge Deep @ Pebble | Brute Force (Tuhin Mehta) | DJ Arpan (Opening)
Champagne Showers - DJs Panic & Nyth @ Blue Waves
THURSDAY PAST AND PRESENT @ Hint
我希望我的輸出像這樣,
CIRRUS LADIES NIGHT with DJ ROHIT
4th of JULY Party ft. DJ JASMEET @ I-Bar
Submerge Deep @ Pebble | Brute Force (Tuhin Mehta) | DJ Arpan (Opening)
Champagne Showers - DJs Panic & Nyth @ Blue Waves
THURSDAY PAST AND PRESENT @ Hint
我試過gsub /^$\\n/,''
, gsub(/\\n/,'')
, squeeze("\\n")
並delete! "\\n"
delete! "\\n"
無濟於事。
另外,我忘了提到我的字符串以空行開頭, #
表示第一行之前的空行,如果這會改變任何東西。
我的String.inspect請求,字符串的內容已更改,但問題仍然相同。
string.inspect :
"\n\n\t\t\t\t\t\t\t\t\t"
"Tricky Tuesdays with DJ John @ Blend"
"\n\n\t\t\t\t\t\t\t\t\t"
"Bladder Buster Challenge with DJ Sean @ Star Rock"
"\n\n\t\t\t\t\t\t\t\t\t"
"Classic Rock Tuesday @ 10D - Chennai"
"\n\n\t\t\t\t\t\t\t\t\t"
"Vodka Night with DJ John @ Blend"
"\n\n\t\t\t\t\t\t\t\t\t"
"\"BOLLYWOOD WEDNESDAYS\" with DJ D Nash @ Candy Club"
"\n\n\t\t\t\t\t\t\t\t\t"
"RE - LAUNCH WEDNESDAY LADIES NIGHT @ ZODIAC"
"\n\n\t\t\t\t\t\t\t\t\t"
"Ladies Night @ 10 D - Chennai"
"\n\n\t\t\t\t\t\t\t\t\t"
"Wednesday Mayhem @ Dublin"
"\n\n\t\t\t\t\t\t\t\t\t"
這是我的解決方案:
text.gsub(/\n+|\r+/, "\n").squeeze("\n").strip
這將刪除所有連續的空行:
result = s.squeeze("\r\n").gsub(/(\r\n)+/, "\r\n")
或沒有Ruby的命令行選項:
grep -v "^$" <file>
首先,您的代碼會刪除所有換行符,而不僅僅是空白換行符 - 這聽起來並不像您想要的那樣。
其次,操作系統歷來不同意怎么能代表換行符-使用的舊的Mac \\r
新線,Linux和OSX使用\\n
,和Windows使用組合\\r\\n
。 所以你真的想用一個\\n
替換連續的\\r
和\\n
s(用那里的空行表示)。
.split(/\\n/).reject{ |l| l.chomp.empty? }.join("\\n")
僅適用於Unix風格:
.split(/\\n/).reject(&:empty?).join("\\n")
也刪除空白行(Unix,Rails方法):
.split(/\\n/).reject(&:blank?).join("\\n")
這是一個刪除所有空白行的正則表達式,包括文件開頭或結尾處的空行,包括僅包含空格或制表符的行,並允許所有三種形式的行結束標記( \\r\\n
, \\n
和\\r
)):
def remove_blank_lines( str, line_ending="\n" )
str.gsub(/(?<=\A|#{line_ending})[ \t]*(?:#{line_ending}|\z)/,'')
end
測試:
[ "\r\n", "\n", "\r" ].each do |marker|
puts '='*70, "Lines ending with: #{marker.inspect}", '='*70
[ "", " ", "\t", " \t", "\t " ].each do |whitespace|
0.upto(2) do |lines|
blank_lines = "#{whitespace}#{marker*lines}"
s = "#{marker*lines}a#{marker*lines}b#{blank_lines}c#{blank_lines}"
tight = remove_blank_lines(s, marker)
puts "%43s -> %s" % [s.inspect, tight.inspect]
end
end
end
#=> ======================================================================
#=> Lines ending with: "\r\n"
#=> ======================================================================
#=> "abc" -> "abc"
#=> "\r\na\r\nb\r\nc\r\n" -> "a\r\nb\r\nc\r\n"
#=> "\r\n\r\na\r\n\r\nb\r\n\r\nc\r\n\r\n" -> "a\r\nb\r\nc\r\n"
#=> "ab c " -> "ab c "
#=> "\r\na\r\nb \r\nc \r\n" -> "a\r\nb \r\nc \r\n"
#=> "\r\n\r\na\r\n\r\nb \r\n\r\nc \r\n\r\n" -> "a\r\nb \r\nc \r\n"
#=> "ab\tc\t" -> "ab\tc\t"
#=> "\r\na\r\nb\t\r\nc\t\r\n" -> "a\r\nb\t\r\nc\t\r\n"
#=> "\r\n\r\na\r\n\r\nb\t\r\n\r\nc\t\r\n\r\n" -> "a\r\nb\t\r\nc\t\r\n"
#=> "ab \tc \t" -> "ab \tc \t"
#=> "\r\na\r\nb \t\r\nc \t\r\n" -> "a\r\nb \t\r\nc \t\r\n"
#=> "\r\n\r\na\r\n\r\nb \t\r\n\r\nc \t\r\n\r\n" -> "a\r\nb \t\r\nc \t\r\n"
#=> "ab\t c\t " -> "ab\t c\t "
#=> "\r\na\r\nb\t \r\nc\t \r\n" -> "a\r\nb\t \r\nc\t \r\n"
#=> "\r\n\r\na\r\n\r\nb\t \r\n\r\nc\t \r\n\r\n" -> "a\r\nb\t \r\nc\t \r\n"
#=> ======================================================================
#=> Lines ending with: "\n"
#=> ======================================================================
#=> "abc" -> "abc"
#=> "\na\nb\nc\n" -> "a\nb\nc\n"
#=> "\n\na\n\nb\n\nc\n\n" -> "a\nb\nc\n"
#=> "ab c " -> "ab c "
#=> "\na\nb \nc \n" -> "a\nb \nc \n"
#=> "\n\na\n\nb \n\nc \n\n" -> "a\nb \nc \n"
#=> "ab\tc\t" -> "ab\tc\t"
#=> "\na\nb\t\nc\t\n" -> "a\nb\t\nc\t\n"
#=> "\n\na\n\nb\t\n\nc\t\n\n" -> "a\nb\t\nc\t\n"
#=> "ab \tc \t" -> "ab \tc \t"
#=> "\na\nb \t\nc \t\n" -> "a\nb \t\nc \t\n"
#=> "\n\na\n\nb \t\n\nc \t\n\n" -> "a\nb \t\nc \t\n"
#=> "ab\t c\t " -> "ab\t c\t "
#=> "\na\nb\t \nc\t \n" -> "a\nb\t \nc\t \n"
#=> "\n\na\n\nb\t \n\nc\t \n\n" -> "a\nb\t \nc\t \n"
#=> ======================================================================
#=> Lines ending with: "\r"
#=> ======================================================================
#=> "abc" -> "abc"
#=> "\ra\rb\rc\r" -> "a\rb\rc\r"
#=> "\r\ra\r\rb\r\rc\r\r" -> "a\rb\rc\r"
#=> "ab c " -> "ab c "
#=> "\ra\rb \rc \r" -> "a\rb \rc \r"
#=> "\r\ra\r\rb \r\rc \r\r" -> "a\rb \rc \r"
#=> "ab\tc\t" -> "ab\tc\t"
#=> "\ra\rb\t\rc\t\r" -> "a\rb\t\rc\t\r"
#=> "\r\ra\r\rb\t\r\rc\t\r\r" -> "a\rb\t\rc\t\r"
#=> "ab \tc \t" -> "ab \tc \t"
#=> "\ra\rb \t\rc \t\r" -> "a\rb \t\rc \t\r"
#=> "\r\ra\r\rb \t\r\rc \t\r\r" -> "a\rb \t\rc \t\r"
#=> "ab\t c\t " -> "ab\t c\t "
#=> "\ra\rb\t \rc\t \r" -> "a\rb\t \rc\t \r"
#=> "\r\ra\r\rb\t \r\rc\t \r\r" -> "a\rb\t \rc\t \r"
嘗試
/^\n/
並用空字符串替換。
你確定你的換行符只是\\n
嗎? 如果不試試
/^\r?\n/
允許換行序列\\r\\n
。
根據@ Tom的答案,這是一個丑陋的黑客:
result = s.squeeze("\r\n").tap{ |s2| :go while s2.gsub!("\r\n\r\n","\r\n") }
它支持DOS( \\r\\n
),Unix( \\n
)和MacOS 9-( \\r
)換行符。 測試:
[ "\r\n", "\n", "\r" ].each do |marker|
1.upto(5) do |lines|
s = "a#{marker*lines}b"
tight = s.squeeze("\r\n").tap{ |s2| :go while s2.gsub!("\r\n\r\n","\r\n") }
puts "%24s -> %s" % [s.inspect, tight.inspect]
end
end
#=> "a\r\nb" -> "a\r\nb"
#=> "a\r\n\r\nb" -> "a\r\nb"
#=> "a\r\n\r\n\r\nb" -> "a\r\nb"
#=> "a\r\n\r\n\r\n\r\nb" -> "a\r\nb"
#=> "a\r\n\r\n\r\n\r\n\r\nb" -> "a\r\nb"
#=> "a\nb" -> "a\nb"
#=> "a\n\nb" -> "a\nb"
#=> "a\n\n\nb" -> "a\nb"
#=> "a\n\n\n\nb" -> "a\nb"
#=> "a\n\n\n\n\nb" -> "a\nb"
#=> "a\rb" -> "a\rb"
#=> "a\r\rb" -> "a\rb"
#=> "a\r\r\rb" -> "a\rb"
#=> "a\r\r\r\rb" -> "a\rb"
#=> "a\r\r\r\r\rb" -> "a\rb"
請注意,這假設您的空白行是真正空白的,並且它們上沒有任何空格。 如果是這種情況,你可以預先執行s.gsub(/^[ \\t]+$/,'')
這樣做: .gsub(/(\\n\\s*\\n)+/, "\\n")
如果需要,用[\\n|\\r
]替換\\n
則表達式中的[\\n|\\r
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.