Ruby REGEX split, any issues with the code

rupeshj

I am a rookie in Regex for Ruby. I read some tutorials and evaluated a piece of code. Please let me know if I can do it in a better way.

Here is my text which needs to be split at {iwsection(*)} and {{usersection}}

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

Here is the ruby regex code I was able to manage.

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

Thank You.

The Desired Output : A array as,

    [ '{{iwsection(1)}}', 'This has some sample text 1\nThis has some sample text 1 - line 2',
    '{{iwsection(2)}}', 'This has some sample text 2',
    '{{iwsection(3)}}', 'This has some sample text 3',
    '{{usersection}}', 'This is a user section\nThis has some sample text\nThis has some sample text.']

With this I will build a Hash,

    { 
    '{{iwsection(1)}}' => 'This has some sample text 1\nThis has some sample text 1 - line 2',
    '{{iwsection(2)}}' => 'This has some sample text 2',
    '{{iwsection(3)}}' => 'This has some sample text 3',
    '{{usersection}}' => 'This is a user section\nThis has some sample text\nThis has some sample text.'
    }

Edit: .....

The code.

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

Using String#scan:

> t.scan(/{{([^}]*)}}\r?\n(.*?)\r?(?=\n{{|\n?$)/)
=> [["iwsection(1)", "This has some sample text 1"], ["iwsection(2)", "This has some sample text 2"], ["iwsection(3)", "This has some sample text 3"], ["usersection", "This is a user section."]]

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

> h.values
=> ["This has some sample text 1", "This has some sample text 2", "This has some sample text 3", "This is a user section."]

> h.keys
=> ["iwsection(1)", "iwsection(2)", "iwsection(3)", "usersection"]

> h["usersection"]
=> "This is a user section."

Update:

#!/usr/bin/env ruby
t = "{{iwsection(1)}}\nThis has some sample text 1 - line 1\nThis has some sample text 1 - line 2\n{{iwsection(2)}}\nThis has some sample text 2\n{{iwsection(3)}}\nThis has some sample text 3\nThis has some sample text\nThis has some sample text\n{{usersection}}\nThis is a user section.\nThis has some sample text\nThis has some sample text"
h = t.chomp.split(/\n/).inject([]) do |a, v|
  if v =~ /{{.*}}/
    a << [v.gsub(/^{{|}}$/, ""), []]
  else
    a.last[1] << v
  end
  a
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 some sample text 1 - line 1\nThis has some sample text 1 - line 2", "iwsection(2)"=>"This has some sample text 2", "iwsection(3)"=>"This has some sample text 3\nThis has some sample text\nThis has some sample text", "usersection"=>"This is a user section.\nThis has some sample text\nThis has some sample text"}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Ruby ReGex Split在第n条新行

来自分类Dev

Is there any difference between \S and [^\s] in Ruby Regex statements?

来自分类Dev

javascript split()使用regEx

来自分类Dev

Ruby regex: operator and

来自分类Dev

Ruby Regex组替换

来自分类Dev

Ruby Regex“包含”

来自分类Dev

Ruby Regex多次匹配

来自分类Dev

Split text with a single code

来自分类Dev

Java在“ split(regEx)”和“ split(regEx,0)”之间的区别?

来自分类Dev

Are there any issues with using nested functions with eventlisteners?

来自分类Dev

Regex performance issues with possible back tracking?

来自分类Dev

Working with Named Regex Groups in Ruby

来自分类Dev

使用Regex的CRUD文本-Ruby

来自分类Dev

Ruby Regex:解析C ++类

来自分类Dev

Ruby regex search and replace array

来自分类Dev

Ruby REGEX解析器

来自分类Dev

Ruby Regex:拒绝选定的URL

来自分类Dev

Ruby Regex,拼接地址

来自分类Dev

Ruby Regex匹配意外字符

来自分类Dev

在ruby中更好的替代join()和split()

来自分类Dev

Java Split RegEx无法正常运作

来自分类Dev

Java Regex / Split不会丢失字符

来自分类Dev

PHP Regex使用preg_split

来自分类Dev

Java Regex .split CFG的字符串

来自分类Dev

Java Regex / Split不会丢失字符

来自分类Dev

Regex.Split忽略空结果

来自分类Dev

Java中的split(),indexOf()和regex混淆了

来自分类Dev

Java Split RegEx无法正常运作

来自分类Dev

C#Regex.Split(如何建立)