How can i be able to ensure that a selected date time value is between a given time range. i.e
2/2/2011 8:10:30 is in开发者_JAVA技巧valid but
2/2/2011 8:30:00 is a valid date
Regular expressions match strings, not numbers or number ranges. Therefore you need to think about the textual representation of all valid times/dates and analyze them.
A number between 5 and 11 would therefore be 1[01]|[5-9]
etc.; this can get arbitrarily complex with dates, especially if you need to validate user input. Then your regex needs to know about leap years and all that - all of which is possible but nightmarish to maintain. So you really need to think about if it's really a regex you want...
For the range 8:30:00-13:00:00
you get (written here as a verbose regex):
\b # start of word
(?: # Either match...
13:00:00 # 13:00:00
| # or
(?: # hours:
1[012] # 10-12
| # or
0?9 # 9, optional leading 0
) # end of hours
: # colon
[0-5][0-9] # minutes: 00-59
: # colon
[0-5][0-9] # seconds
| # or
0?8 # hour: (0)8
: # colon
[3-5][0-9] # minutes 30-59
: # colon
[0-5][0-9] # seconds
) # end of alternation.
\b # end of word
See why this isn't a good idea?
精彩评论