Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two alternatives:</p> <pre><code>s = "[test| blah] \n [foo |bar bar bar]\n[test| abc |123 | 456 789]" s.split(/\s*\n\s*/).map{ |p| p.scan(/[^|\[\]]+/).map(&amp;:strip) } #=&gt; [["test", "blah"], ["foo", "bar bar bar"], ["test", "abc", "123", "456 789"]] irb&gt; s.split(/\s*\n\s*/).map do |line| line.sub(/^\s*\[\s*/,'').sub(/\s*\]\s*$/,'').split(/\s*\|\s*/) end #=&gt; [["test", "blah"], ["foo", "bar bar bar"], ["test", "abc", "123", "456 789"]] </code></pre> <p>Both of them start by splitting on newlines (throwing away surrounding whitespace).</p> <p>The first one then splits each chunk by looking for anything that is not a <code>[</code>, <code>|</code>, or <code>]</code> and then throws away extra whitespace (calling <code>strip</code> on each).</p> <p>The second one then throws away leading <code>[</code> and trailing <code>]</code> (with whitespace) and then splits on <code>|</code> (with whitespace).</p> <hr> <p>You cannot get the final result you want with a single <code>scan</code>. About the closest you can get is this:</p> <pre><code>s.scan /\[(?:([^|\]]+)\|)*([^|\]]+)\]/ #=&gt; [["test", " blah"], ["foo ", "bar bar bar"], ["123 ", " 456 789"]] </code></pre> <p>…which drops information, or this:</p> <pre><code>s.scan /\[((?:[^|\]]+\|)*[^|\]]+)\]/ #=&gt; [["test| blah"], ["foo |bar bar bar"], ["test| abc |123 | 456 789"]] </code></pre> <p>…which captures the contents of each "array" as a single capture, or this:</p> <pre><code>s.scan /\[(?:([^|\]]+)\|)?(?:([^|\]]+)\|)?(?:([^|\]]+)\|)?([^|\]]+)\]/ #=&gt; [["test", nil, nil, " blah"], ["foo ", nil, nil, "bar bar bar"], ["test", " abc ", "123 ", " 456 789"]] </code></pre> <p>…which is hardcoded to a maximum of four items, and inserts <code>nil</code> entries that you would need to <code>.compact</code> away.</p> <p>There is no way to use Ruby's <code>scan</code> to take a regex like <code>/(?:(aaa)b)+/</code> and get multiple captures for each time the repetition is matched.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload