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