开发者

Ruby gsub issues

开发者 https://www.devze.com 2023-03-31 15:36 出处:网络
I have a piece of text that resembled the following: ==EXCLUDE #lots of lines of text ==EXCLUDE #this is what I actually want

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

精彩评论

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

关注公众号