How to show only the matching portions (mimicing the "-o, --only-matching" grep option) in Ruby?
<p>Unix <strong>grep</strong> has a simple <code>-o, --only-matching</code> option to show only the part of a matching line that matches PATTERN.</p>

<p>What is the simplest way to mimic that behaviour in Ruby, either using the grep method or any other standard method (without exec or gems)?</p>

<p>Example given:</p>

<pre><code>File.open('test.css','r') do |file|
  file.each_line do |line|
    match=/p[A-Z]+/.match(line)
    puts match.to_s unless match.nil?
  end
end
</code></pre>

<p>would mimic <code>grep -oE "p[A-Z]+" test.css</code>.
I suppose there are better (cleaner and more concise) ways.</p>