title: Regex to extract time (e.g. 7:30pm, 8 pm, 9.05) from string
body: <p>I'm working on a Rails application which uses an external feed for some event data and annoyingly they only provide a string with the time in. For example:</p>
  
  <blockquote>
    <p>Doors open at 7:30pm, show starts at 9pm</p>
  </blockquote>
  
  <p>I'm aiming to extract the first time from these strings and put it into a datetime field. The system needs to capture the following kinds of values:</p>
  
  <ol>
  <li>11 am</li>
  <li>12pm</li>
  <li>1pm</li>
  <li>2:15pm</li>
  <li>3.30pm</li>
  <li>4.45</li>
  <li>5:30</li>
  <li>06:15</li>
  <li>07:30pm</li>
  <li>8:30 pm</li>
  <li>9.15 pm</li>
  </ol>
  
  <p>But not these ones:</p>
  
  <ol start="12">
  <li>105</li>
  <li>2 50</li>
  <li>305pm</li>
  <li>4 15pm</li>
  <li>74pm</li>
  <li>840am</li>
  </ol>
  
  <p>I figure the best way to do this is with regex and through some searching (and particularly <a href="https://stackoverflow.com/questions/12504012/regex-to-find-time-in-a-string">this SO question</a>) I've got the following:</p>
  
  <p><code>[0-9]{1,2}(:|.)??[0-9]{0,2}\s?(am|pm|AM|PM)</code></p>
  
  <p>It partly works but doesn't exclude any of the ones I don't want and seems to only capture the first character of am/pm in 2 and 3.</p>
  
  <p>Is this possible with regex?</p>
  
  <p>Thanks!</p>