Forum: Ruby problem replacing newlines in regexp

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ce5990549a8185ad45643be2cfbff9b5?d=identicon&s=25 Mike Steiner (Guest)
on 2007-04-30 04:05
(Received via mailing list)
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 Steiner
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry (Guest)
on 2007-04-30 04:16
(Received via mailing list)
On 4/30/07, Mike Steiner <mikejaysteiner@gmail.com> 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
Caf38c89d40443a858741b61ac6d82de?d=identicon&s=25 Dan Zwell (Guest)
on 2007-04-30 04:58
(Received via mailing list)
Harry wrote:
> Harry
>

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

Dan
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry (Guest)
on 2007-04-30 05:10
(Received via mailing list)
On 4/30/07, Dan Zwell <dzwell@gmail.com> 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
Ce5990549a8185ad45643be2cfbff9b5?d=identicon&s=25 Mike Steiner (Guest)
on 2007-04-30 19:57
(Received via mailing list)
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/ ,
" " )?
Caf38c89d40443a858741b61ac6d82de?d=identicon&s=25 Dan Zwell (Guest)
on 2007-04-30 21:15
(Received via mailing list)
Mike Steiner 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
This topic is locked and can not be replied to.