lundi 3 août 2015

Ruby split keep the delimiter before the string

I have the following string :

a = '% abc \n %% abcd \n %% efgh\n '

I would like the ouput to be

['% abc \n', '%% abcd \n', '%% efgh \n']

If I have

b = '%% abc \n %% efg \n %% ijk \n]

I would like the output to be

['%% abc \n', '%% efg \n', '%% ijk \n']

I use b.split('%%').collect!{|v| '%%' + v } and it works fine for case 2.

but it doesn't work for case 1.

I saw some post of using 'scan' or 'split' to keep the delimiter if its after the string

For example : 'a; b; c' becomes ['a;', 'b;' ,'c']

But I want the opposite ['a', ';b', ';c']

Aucun commentaire:

Enregistrer un commentaire