title: Regex optional match operator: how to apply to a string of characters?
body: <p>I am facing an issue when trying to apply the following regular expression:</p>
  
  <p><code>(1234).*?(abcd)?</code></p>
  
  <p>To the below string:</p>
  
  <p><code>1234567abcd</code></p>
  
  <p>My expectation is that the above should produce two matches:</p>
  
  <ol>
  <li>1234</li>
  <li>abcd</li>
  </ol>
  
  <p>However, this does not work.  You might suggest "well, just remove the trailing <code>?</code> character?" -- but I want this second pattern to be optional.</p>
  
  <p>How is this done?</p>
  
  <p>To reiterate:</p>
  
  <p><code>(1234).*?(abcd)</code></p>
  
  <p>... gives the desired result, but my searched string won't always contain <code>abcd</code>.</p>
  
  <p>In case anyone is wondering, this is a simplified example of the bigger picture problem.  I'll explain that if needed.</p>
  
  <p>====</p>
  
  <p>I think this problem needs additional clarification.  Here is a more complete example of what I am trying to do, in ruby.</p>
  
  <p>Given the following two "doctored up" lines from my logfile:</p>
  
  <pre><code>Aug 28 00:00:05 app-system-1 app-prod[7660]: Completed 200 OK in 150ms (Views: 24.6ms | ActiveRecord: 66.1ms)
  Aug 28 00:05:06 app-system-1 app-prod[10639]: Completed 302 Found in 81ms (ActiveRecord: 74.6ms)
  </code></pre>
  
  <p>I have tried to compile a regex in ruby as follows:</p>
  
  <pre><code>d=Regexp.new('(?&lt;timestamp&gt;\w{1,3}\s\d{1,2}\s\d\d:\d\d:\d\d).*(?&lt;in&gt;in [0-9]*).*(?&lt;views&gt;Views: [0-9]*).*(?&lt;activerecord&gt;ActiveRecord: [0-9]*)')
  </code></pre>
  
  <p>Obviously in some cases the the 'views' text will be included, in other rows, it is not present.</p>
  
  <p>I want to be able to do something like:</p>
  
  <pre><code>v=d.match(line)
  if !v.nil?
      puts v[:timestamp]+ " " + v[:in] + " " + v[:views] + " " + v[:activerecord]
  </code></pre>
  
  <p>This is obviously an incomplete example but hopefully this clarifies.</p>