title: Regex to match mm/dd/yyyy hh:mm:ss AM or PM
body: <p>I have a program that creates information for a time stamp, the time stamp has to match the following format: <code>MM/DD/YYYY HH:MM:SS AM or PM</code></p>
  
  <p>For example:</p>
  
  <p><code>06/02/2016 10:14:56 AM</code></p>
  
  <p><code>09/14/2014 9:16:32 PM</code></p>
  
  <p>I need a way to match this information using preferable a regex, however it doesn't have to be a regex. Any help with this would be greatly appreciated.</p>
  
  <p>I know how to match the digits <code>/\d+/</code>, and non white space characters, however matching the digits won't grab the <code>:</code>, <code>/</code>, <code>AM or PM</code>. And grabbing the non white space will grab everything except for the obviously white space. </p>
  
  <p>So to recap I need a way to verify that a formatted time and date: <code>MM/DD/YYYY HH:MM:SS AM or PM</code> That can contain 20 to 22 characters (including white space) exists or not given as user input.</p>
  
  <p>Example of what I need:</p>
  
  <pre><code>def timestamp
    print 'Enter the time stamp to verify: '
    ts = gets.chomp
    if !(ts[REGEX-HERE])
      puts 'Invalid formatting of time stamp'
      exit 1
    else
      puts ts
    end
  end
  
  # &lt;= 6/1/2016 1:10:5 PM will output '6/1/2016 1:10:5 PM'
  # &lt;= 06/01/2016 01:10:05 PM will output 'Invalid formatting of time stamp'
  # &lt;= 10/1/2016 1:25:43 AM will output '10/1/2016 1:25:43 AM'
  # &lt;= 10/01/2016 01:25:43 AM will output 'Invalid formatting of time stamp'
  </code></pre>