Regex to match mm/dd/yyyy hh:mm:ss AM or PM

后端 未结 5 1256
逝去的感伤
逝去的感伤 2021-01-22 16:57

I have a program that creates information for a time stamp, the time stamp has to match the following format: MM/DD/YYYY HH:MM:SS AM or PM

For example:

相关标签:
5条回答
  • 2021-01-22 17:23

    This regex should work for you for validation of your inputs:

    ^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z
    

    RegEx Demo

    This will validate either of these inputs lines:

    10/22/2016 9:15:32 PM
    9/9/2016 9:9:5 AM
    
    0 讨论(0)
  • 2021-01-22 17:28

    Characters like / and :, and even letters can be added litterally. Try this:

    /[01][0-9]/[0-3][0-9]/[0-9]{4} [0-2]?[0-9]:[0-6]?[0-9]:[0-6]?[0-9] (AM|PM)/
    
    0 讨论(0)
  • 2021-01-22 17:32

    Like stated by mu is too short, regex is great and all but you need more in your arsenal then just that.

    def timestamp
      print 'Enter time stamp: '
      ts = gets.chomp.upcase
      verify = DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
      puts ts
    rescue ArgumentError
      puts 'Invalid timestamp formatting must contain date time AM or PM'
      timestamp
    end
    

    Of course this could cause an overflow from the buffer because it's recursive, but that chances of that happening are pretty slim.

    When this is run:

    irb(main):01:0> def timestamp
    irb(main):02:1>   print 'Enter time stamp: '
    irb(main):02:1>   ts = gets.chomp.upcase
    irb(main):04:1>   verify = DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
    irb(main):05:1>   puts ts
    irb(main):06:1> rescue ArgumentError
    <valid timestamp formatting must contain date time AM or PM'
    irb(main):07:1>   timestamp
    irb(main):08:1> end
    => :timestamp
    irb(main):09:0> timestamp
    Enter time stamp: test
    Invalid timestamp formatting must contain date time AM or PM
    Enter time stamp: 06/30/2016 6:6:54 pm
    06/30/2016 6:6:54 PM
    => nil
    

    However it is possible that you could make a better way of checking:

    def enter_stamp
      print 'Enter timestamp: '
      ts = gets.chomp
      verify_stamp(ts)
    end
    
    def verify_stamp(timestamp)
      Date.strptime(timestamp, '%m/%d/%Y %I:%M:%S %p')
      puts timestamp unless ArgumentError
    rescue ArgumentError
      puts 'Invalid timestamp format'
      enter_stamp
    end
    

    This makes it so that the methods are more OOPish, and Ruby is all about OOP so it kind of works. When this is run:

    irb(main):001:0> require 'date'
    => true
    irb(main):002:0>
    irb(main):003:0* def enter_stamp
    irb(main):004:1>   print 'Enter timestamp: '
    irb(main):005:1>   ts = gets.chomp
    irb(main):006:1>   verify_stamp(ts)
    irb(main):007:1> end
    => :enter_stamp
    irb(main):008:0>
    irb(main):009:0* def verify_stamp(timestamp)
    irb(main):010:1>   Date.strptime(timestamp, '%m/%d/%Y %I:%M:%S %p')
    irb(main):011:1>   puts timestamp unless ArgumentError
    irb(main):012:1> rescue ArgumentError
    irb(main):013:1>   puts 'Invalid timestamp format'
    irb(main):014:1>   enter_stamp
    irb(main):015:1> end
    => :verify_stamp
    irb(main):016:0> enter_stamp
    Enter timestamp: test
    Invalid timestamp format
    Enter timestamp: 06/12/2016 8:08:54 pm
    => nil
    

    And of course you could also use just a regex:

    def verify_timestamp(stamp)
      if !(stamp[/^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z/])
        puts 'Invalid timestamp formatting'
        timestamp
      else
        stamp
      end
    end
    
    def timestamp
      print 'Enter timestamp for removal: '
      ts = gets.chomp
      verify_timestamp(ts)
    end
    

    As I said regexs are wonderful, they just aren't exactly the best thing to use all the time.. When this is run:

    irb(main):001:0> def verify_timestamp(stamp)
    <mp[/^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} [AP]M\z/])
    irb(main):003:2>     puts 'Invalid timestamp formatting'
    irb(main):004:2>     timestamp
    irb(main):005:2>   else
    irb(main):006:2*     stamp
    irb(main):007:2>   end
    irb(main):008:1> end
    => :verify_timestamp
    irb(main):009:0>
    irb(main):010:0* def timestamp
    irb(main):011:1>   print 'Enter timestamp for removal: '
    irb(main):012:1>   ts = gets.chomp
    irb(main):013:1>   verify_timestamp(ts)
    irb(main):014:1> end
    => :timestamp
    irb(main):015:0> verify_timestamp(timestamp)
    Enter timestamp for removal: test
    Invalid timestamp formatting
    Enter timestamp for removal: 06/12/2016 8:18:54 pm
    => "06/12/2016 8:18:54 pm"
    irb(main):016:0>
    

    So there you have it, there are three different ways to verify your time stamps, depending on what you want to use them for might be another story, but any of these three should do the trick.

    0 讨论(0)
  • 2021-01-22 17:34

    Try this . I hope i understood your question. Tested here http://rubular.com/ and it works.

      ^(\d{1,2}\/){2}\d{4}\s+((\d+)(\:)){2}\d+\s+(AM|PM)
    

    0 讨论(0)
  • 2021-01-22 17:35

    I don't think a regex is the right tool for this job. I'd use DateTime.strptime, give it the format you're expecting and let it deal with all the "50 hours is invalid in a time of day", "09 versus 9", ... issues.

    Something like:

    require 'date'
    
    def timestamp
      print 'Enter the time stamp to verify: '
      ts = gets.chomp # You might want to include a String#strip call in here too
      DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
      puts ts
    rescue ArgumentError
      puts 'Invalid formatting of time stamp'
      exit 1
    end
    

    Regexes are nice but they shouldn't be the only parsing tool you have.

    0 讨论(0)
提交回复
热议问题