Marshall Shen
Marshall Shen

Reputation: 1333

Replace the pipe character "|" with line breaks?

I am trying to use Regex in my Ruby program to convert "|" character into a line breaker, so for example:

# convert("title|subtitle") => "title \n subtitle"

The regex I'm trying is the following:

title_params =~ s/\|/\\n/

But I kept getting errors saying that "|" is not recognized.

Upvotes: 1

Views: 1046

Answers (2)

DGM
DGM

Reputation: 26979

Regex is not needed for this simple problem:

=> puts "foo|bar".tr("|","\n")
foo
bar

Upvotes: 8

oldergod
oldergod

Reputation: 15010

I don't really know the syntax of your way of doing this but this works fine for me.

>> a = "title | subtitle"
=> "title | subtitle"
>> a.gsub(/\|/,"\n")
=> "title \n subtitle"

Upvotes: 2

Related Questions