# Substitutions without eating newlines

**URL:** <https://www.ruby-forum.com/t/substitutions-without-eating-newlines/104585>\
**Category:** Rails\
**Created:** [May 19, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/substitutions-without-eating-newlines/104585 "2007-05-19T23:31:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Whitedot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/22d042/32.png) [@Whitedot](https://www.ruby-forum.com/u/Whitedot)\
**Post date:** [May 19, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/substitutions-without-eating-newlines/104585/1 "2007-05-19T23:31:31Z")

</div>

I’m writing this simple substitution method and I have a   
providing me with the input text. I have some multiline text:

This is  
a _test_  
ok?

Which I pass through to my async method in the variable @text.  
In the method, my regular expression and gsub matches anything between  
asterisks, and put it in brackets:

@text.gsub!(/\*(.\*?)\*/m, ‘\<\1\>’)

And this yields:

This is a ok?

How can I get my regexp to stop eating the newlines? As you can see, I  
have the multiline flag on. Am I missing something within the actual  
regexp? Help would be greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Whitedot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/22d042/32.png) [@Whitedot](https://www.ruby-forum.com/u/Whitedot)\
**Post date:** [May 20, 2007, 5:58am UTC](https://www.ruby-forum.com/t/substitutions-without-eating-newlines/104585/2 "2007-05-20T05:58:52Z")

</div>

This is odd, as running in irb yields the following:

irb(main):001:0\> s = “This is\na _test_\nok?”  
=\> “This is\na _test_\nok?”  
irb(main):002:0\> s.gsub(/\*(._?)\*/m, ‘\<\1\>’)  
=\> “This is\na \nok?”  
irb(main):002:0\> puts s.gsub(/\*(._?)\*/m, ‘\<\1\>’)  
This is  
a   
ok?

Here is my Ruby version:

david@redclay ~ $ ruby -v  
ruby 1.8.5 (2006-08-25) [i686-linux]

I wonder why it works for me in irb, but not for you in your controller?  
I  
also ran it in script/console with Rails 1.2.3, and everything was  
golden.

---

<div class="post-metadata">

**Author:** ![Whitedot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/22d042/32.png) [@Whitedot](https://www.ruby-forum.com/u/Whitedot)\
**Post date:** [May 20, 2007, 5:58am UTC](https://www.ruby-forum.com/t/substitutions-without-eating-newlines/104585/3 "2007-05-20T05:58:53Z")

</div>

dear sender,  
i´m out of the office until may 29th.  
your email will not be forwarded.  
for urgent stuff please contact removed\_email\_address@domain.invalid  
kind regards,  
alexander
