只在ruby中保留空白换行符的最佳方法

时间:2016-08-28 01:40:06

标签: ruby regex

我正在制作一种解析类似降价文字的简单方法。让我们假装我的字符串看起来像这样(显示的是\ n字符)

hello this\n
is part of the same paragraph\n
\n
this is a separate paragraph\n
\n
\n
\n
this is another one!\n

现在,我每行添加一个新的<p>代码,最终看起来像这样 -

<p>hello this</p>
<p>is part of the same paragraph</p>
<p></p>
<p>this is a separate paragraph</p>
<p></p>
<p></p>
<p></p>
<p>this is another one!</p>

我在ruby中使用.squeeze("\n")方法稍微减少了一点。然后我的HTML看起来像这样 -

<p>hello this</p>
<p>is part of the same paragraph</p>
<p>this is a separate paragraph</p>
<p>this is another one!</p>

正如您所看到的,这消除了额外的p元素 - 但前两行仍然分为段落。

如何实现类似于降价的效果,其中新段落需要两次返回? e.g。

this is 
part of the same paragraph

new para!

变为

this is part of the same paragraph
\n
new para!

变成......

<p>this is part of the same paragraph</p>
<p>new para!</p>

是否存在我忘记的正则表达式解决方案?或者

2 个答案:

答案 0 :(得分:1)

这是一个快速的想法:

str = <<-STR
hello this
is part of the same paragraph

this is a separate paragraph



this is another one!
STR

result = ''

result << '<p>'
result << str.gsub!(/\n{2,}/, "</p>\n<p>")
result << '</p>'

puts result


# Output
<p>hello this
is part of the same paragraph</p>
<p>this is a separate paragraph</p>
<p>this is another one!
</p>

答案 1 :(得分:0)

您可以使用gsub的块版本:

str = "hello this
is part of the same paragraph

this is a separate paragraph



this is another one!
"

str.gsub!(/\n+/) { |newlines| newlines[/\n{2,}/] ? "\n" : " " }
str.each_line { |line| puts "<p>#{line.strip}</p>" }

# output
<p>hello this is part of the same paragraph</p>
<p>this is a separate paragraph</p>
<p>this is another one!</p>