DQuang
DQuang

Reputation: 23

ruby: How to remove the string “\n” right before a number from a string?

I have a Ruby string that looks like:

"AUTO TEST\nJANE DOE-RIGHT\n12097105\nJOE BIN\n1216515\nREGRESSION SUE TEST\n10023436"

Note that there's no number after AUTO TEST. Then I want to turn it into this:

"AUTO TEST\nJANE DOE-RIGHT12097105\nJOE BIN1216515\nREGRESSION SUE TEST10023436" 

Remove \n only it's standing right before a number. The result can be an array or another string.

Anyone can help me with this please?

Upvotes: 0

Views: 54

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521093

You may try replacing \n(?=\d) with empty string:

input = "AUTO TEST\nJANE DOE-RIGHT\n12097105\nJOE BIN\n1216515\nREGRESSION SUE TEST\n10023436"
output = input.gsub(/\n(?=\d)/, '')
puts output

This prints:

AUTO TEST
JANE DOE-RIGHT12097105
JOE BIN1216515
REGRESSION SUE TEST10023436

Upvotes: 2

Related Questions