Regular expression matching 12 hour 24 hour system time

Source: Internet
Author: User

1. Regular expression matching time

(1) 12-hour system-match 8:10 AM or ...

  • The simplest is also the first thing we think of is this kind of regular: [0-9]? [0-9]:[0-9]{2} (AM|PM);

This can really meet the demand, but the above-mentioned regular can match 99:99 am this meaningless data, if only to represent meaningful data??

  • This can be used in this way: (1?[ 0-2]| [1-9]): [0-5][0-9] (AM|PM);

(2) 24-hour system matching time

24-hour system we need to consider 0 of this problem, such as 03:30;

We carefully think about this problem, 24 hours can be divided into these three kinds of situations, 00-09,10-19,20-23;

  • According to this idea we can write: (0[0-9]|1[0-9]|2[0-3]): [0-5][0-9], if there is a 3:30 this situation, you can write (0?[ 0-9]|1[0-9]|2[0-3]): [0-5][0-9]

The above is really satisfying the demand, but can you make the expression more perfect?

  • Regular expression merging: ([01]?[ 0-9]|2[0-3]): [0-5][0-9]

In addition to the method mentioned above, there is another way of thinking can achieve the same effect, but this method is not easy to think of;

Thought two: Divides the time into five kinds of situations, 04-09,14-19,00-03,10-13,20-23;

  • ([0-1]? [4-9]| [0-2]? [0-3])

Regular expression matching 12 hour 24 hour system time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.