title: Parse Lines of Single Words and Groups of Words Inside Quotes Using Regular Expressions in Ruby
body: <p>I'm trying to figure out how to better parse lines of text that have values that look like this:</p>
  
  <pre><code>line1  
  'Line two' fudgy whale 'rolly polly'  
  fudgy 'line three' whale  
  fudgy whale 'line four'  
  'line five' 'fish heads' 
  line six  
  </code></pre>
  
  <p>I wish to use <strong>a single regular expression</strong> to display the desired output. I already know how to kludge it up to get the desired output but <strong>I want a single expression</strong>.</p>
  
  <p><strong>Desired output</strong>:</p>
  
  <pre><code>["line1"]
  ["Line two", "fudgy", "whale", "rolly polly"]
  ["fudgy", "line three", "whale"]
  ["fudgy", "whale", "line four"]
  ["line five", "fish heads"]
  ["line", "six"]
  </code></pre>
  
  <p>The line reading is already handled for me via Cucumber. Each line is read as one string value and I want to parse out single words and any number of words contained inside single quotes. I know less than nothing about regular expressions but I've hobbled together a regular expression using the regex "or" operator ("|") that got me close.</p>
  
  <p>Taking that regex I first tried parsing each line using a string split:</p>
  
  <pre><code>text_line.split(/(\w+)|'(.*?)'/)
  </code></pre>
  
  <p>Which resulted in the following, less than acceptable, arrays:</p>
  
  <pre><code>["", "line1"]
  ["", "Line two", " ", "fudgy", " ", "whale", " ", "rolly polly"]
  ["", "fudgy", " ", "line three", " ", "whale"]
  ["", "fudgy", " ", "whale", " ", "line four"]
  ["", "line five", " ", "fish heads"]
  ["", "line", "", "six"]
  </code></pre>
  
  <p>I next tried using scan instead of a split and I saw this:</p>
  
  <pre><code>text_line.scan(/(\w+)|'(.*?)'/)
  [["line1", nil]]
  [[nil, "Line two"], ["fudgy", nil], ["whale", nil], [nil, "rolly polly"]]
  [["fudgy", nil], [nil, "line three"], ["whale", nil]]
  [["fudgy", nil,], ["whale", nil], [nil, "line four"]]
  [[nil, "line five"], [nil, "fish heads"]]
  [["line", nil], [nil, "six",]]
  </code></pre>
  
  <p>So I could see the regex "or" operator was producing a value for each possible "or" position which made sense. Knowing that I figured out I could use scan, flatten, and compact to clean it up giving me the <strong>desired output</strong>:</p>
  
  <pre><code>text_line.scan(/(\w+)|'(.*?)'/).flatten.compact
  ["line1"]
  ["Line two", "fudgy", "whale", "rolly polly"]
  ["fudgy", "line three", "whale"]
  ["fudgy", "whale", "line four"]
  ["line five", "fish heads"]
  ["line", "six"]
  </code></pre>
  
  <p>But using the scan, flatten, and compact looks incredibly ugly and it seems like I'm just monkey patching my own bad regular expression. I'm thinking instead of ham-handedly fixing the sloppy output from my poorly constructed regex I should just write a better regular expression.</p>
  
  <p>So, <strong>is it possible to use a single regular expression to parse the above lines and get the desired output</strong>? I may be way off on the regex to begin with but I'm thinking if I could just somehow group the or's so they only return one value per group that would probably be what I'm looking for.</p>
  
  <p>Please feel free to suggest alternate solutions but <strong>I'm looking for elegant solutions done the Ruby way</strong> since I'm trying to teach myself how to use the language.</p>
  
  <p>Thanks in advance for your time.</p>
  
  <p><strong>edited to incorporate tininfi's better, more accurate regex</strong></p>