Insert a string after a specific character in Ruby

I need to insert a string after a specific character using Ruby.

For example, if I have a line like the following:

(N D CGYRWIFGD2S7 0 1 N)(N D CGYCGYOVFBK0 0 N N)(ISA N N N CGYCG3FEXOIS N PUB NONE N N 0)(ISA N N N CGYCGYFGAOIS N PUB NONE N N 0)(ISA N N N CGYCG2FGAOIS N PUB NONE N N 0)(N D CGYCGYOVFBK1 0 N N)(N D CGYLOCFGA2S7 0 N N)(N D CGY01TFGD2S7 0 N N)(N D CGY01TCASUAL 0 N N)(N D CGYATTUSAOS7 0 1 N)(ISA N N N CGYAGTAD4OIS N PUB NONE 0 N 7)

I want to add an html tag <br />after each closing brace ")".

I think I can use a regex, but each line has a different number of brackets. So this particular line can have 5, and the other can 20. If my limited Ruby knowledge or programming is at all, I'm looking for help :)

Thanks!

+3
source share
3 answers
s.gsub(')', ')<br />')
+16
source

Use gsubfor global replacement.

my_string.gsub(/\)/, ")<br />");
+8
source

Or, use split and join:

delim = ')'
s.split(delim).join(delim + '<br />')
0
source

Source: https://habr.com/ru/post/1714646/


All Articles