Ruby REGEX split, any issues with the code -


i rookie in regex ruby. read tutorials , evaluated piece of code. please let me know if can in better way.

here text needs split @ {iwsection(*)} , {{usersection}}

    t='{{iwsection(1)}}     has sample text 1 - line 1     has sample text 1 - line 2     {{iwsection(2)}}     has sample text 2     {{iwsection(3)}}     has sample text 3     {{usersection}}     user section.     has sample text     has sample text' 

here ruby regex code able manage.

    t.split(/^({{[i|u][wsection]\w*...}})/) 

thank you.

the desired output : array as,

    [ '{{iwsection(1)}}', 'this has sample text 1\nthis has sample text 1 - line 2',     '{{iwsection(2)}}', 'this has sample text 2',     '{{iwsection(3)}}', 'this has sample text 3',     '{{usersection}}', 'this user section\nthis has sample text\nthis has sample text.'] 

with build hash,

    {      '{{iwsection(1)}}' => 'this has sample text 1\nthis has sample text 1 - line 2',     '{{iwsection(2)}}' => 'this has sample text 2',     '{{iwsection(3)}}' => 'this has sample text 3',     '{{usersection}}' => 'this user section\nthis has sample text\nthis has sample text.'     } 

edit: .....

the code.

    section_array = text.chomp.split(/\r\n|\n/).inject([]) |a, v|     if v =~ /{{.*}}/       << [v.gsub(/^{{|}}$/, ""), []]     else       a.last[1] << v     end         end.select{ |k, v| (k.start_with?("iwsection") || k.start_with?("usersection")) }.map{ |k, v| ["{{#{k}}}", v.join("\n")] } 

using string#scan:

> t.scan(/{{([^}]*)}}\r?\n(.*?)\r?(?=\n{{|\n?$)/) => [["iwsection(1)", "this has sample text 1"], ["iwsection(2)", "this has sample text 2"], ["iwsection(3)", "this has sample text 3"], ["usersection", "this user section."]]  > h = t.scan(/{{([^}]*)}}\r?\n(.*?)\r?(?=\n{{|\n?$)/).to_h => {"iwsection(1)"=>"this has sample text 1", "iwsection(2)"=>"this has sample text 2", "iwsection(3)"=>"this has sample text 3", "usersection"=>"this user section."}  > h.values => ["this has sample text 1", "this has sample text 2", "this has sample text 3", "this user section."]  > h.keys => ["iwsection(1)", "iwsection(2)", "iwsection(3)", "usersection"]  > h["usersection"] => "this user section." 

update:

#!/usr/bin/env ruby t = "{{iwsection(1)}}\nthis has sample text 1 - line 1\nthis has sample text 1 - line 2\n{{iwsection(2)}}\nthis has sample text 2\n{{iwsection(3)}}\nthis has sample text 3\nthis has sample text\nthis has sample text\n{{usersection}}\nthis user section.\nthis has sample text\nthis has sample text" h = t.chomp.split(/\n/).inject([]) |a, v|   if v =~ /{{.*}}/     << [v.gsub(/^{{|}}$/, ""), []]   else     a.last[1] << v   end   end.select{ |k, v| k.start_with? "iwsection" or k === "usersection" }.map{ |k, v| [k, v.join("\n")] }.to_h puts h.inspect 

output:

{"iwsection(1)"=>"this has sample text 1 - line 1\nthis has sample text 1 - line 2", "iwsection(2)"=>"this has sample text 2", "iwsection(3)"=>"this has sample text 3\nthis has sample text\nthis has sample text", "usersection"=>"this user section.\nthis has sample text\nthis has sample text"} 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -