开发者

Ruby gsub issues

I have a piece of text that resembled the following:

==EXCLUDE

#lots of lines of text

==EXCLUDE

#this is what I actually want

And so I was trying to remove the unwanted bit by doing:

str.gsub!(/==EX.*?==EXCLUDE/, '')

However, its not working. When I tried to remove the \n chars fir开发者_JS百科st, it worked like a dream. The issue is that I can't actually remove the \n characters. How can I do a substitution like this while leaving newlines in place?


By default, the . does not match line break chars. If you enable the m modifier in Ruby (in other languages, this is the s modifier) it should work:

str.gsub!(/==EX.*?==EXCLUDE/m, '')

Here's a live demo on Rubular: http://rubular.com/r/YxLSB1Iq95


Try str.gsub!(/==EX.*?==EXCLUDE/m, '')

That should make it span new lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜