# Problem replacing newlines in regexp

**URL:** <https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256>\
**Category:** Ruby\
**Created:** [April 30, 2007, 4:05am UTC](https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256 "2007-04-30T04:05:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 30, 2007, 4:05am UTC](https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256/1 "2007-04-30T04:05:58Z")

</div>

I’m trying to do a gsub ( “\n” , " " ) but none of the newlines are  
replaced. Is there something special I have to do for newlines? (If it  
makes  
a difference, I’m running under Windows.)

Thanks,  
Mike S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 30, 2007, 4:16am UTC](https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256/2 "2007-04-30T04:16:59Z")

</div>

On 4/30/07, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to do a gsub ( “\n” , " " ) but none of the newlines are  
> replaced. Is there something special I have to do for newlines? (If it makes  
> a difference, I’m running under Windows.)

Try gsub!( “\n” , " " )

Harry

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 30, 2007, 4:58am UTC](https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256/3 "2007-04-30T04:58:01Z")

</div>

Harry wrote:

> Harry

You’ve probably gotta specify the multiline match option (the /m at the  
end):  
line.gsub!(/\n/m, " ")

Dan

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 30, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256/4 "2007-04-30T19:57:20Z")

</div>

Okay, here’s the birds-eye view:

I’m converting some database data from pure text to CSV format, and I  
need  
to replace the newlines with something else (a space or a slash).

Sample input record:

Field1=value1  
Field2=value2  
more stuff in Field2  
---------- # end-of-record separator

Sample output record:

FIELD1,FIELD2  
value1,value2 / more stuff in Field2

Does that help?

And the /m option only changes how “.” is treated in a regexp, right?

Can I use somestring.gsub ( /$/ , " " ) instead of somestring.gsub (  
/\n/ ,  
" " )?

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 30, 2007, 5:10am UTC](https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256/5 "2007-04-30T05:10:02Z")

</div>

On 4/30/07, Dan Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Harry
> 
> You’ve probably gotta specify the multiline match option (the /m at the  
> end):  
> line.gsub!(/\n/m, " ")
> 
> Dan

I just noticed the subject said regexp, not string. oops.  
Mike, would you post some code?  
It will make it easier to understand what you are trying to do.

Harry

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 30, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/problem-replacing-newlines-in-regexp/93256/6 "2007-04-30T21:15:24Z")

</div>

Mike S. wrote:

> ---------- # end-of-record separator
> 
> Can I use somestring.gsub ( /$/ , " " ) instead of somestring.gsub ( /\n/ ,  
> " " )?

I guess you’re right that /m isn’t useful here. But gsub(/\n/, " ") does  
seem to do what you want. And you can’t use /$/ here because when you  
try to substitute for $, it doesn’t actually replace the end of the  
line, but adds to it (so what you wrote adds a space to the end of each  
line).

Dan
