Regular Expressions
-
Ok, I am trying to make an Expression to evaluate a Short Time format (hh:mm). I've seen the standard format of "\d\d:\d\d" but I can't use it because it allows values greater than 23 for the Hour. I've come up with one but I need help on the 2nd digit in the hour. Basically in laymen's terms it goes something like this: (1 or 2)(??):(0 - 5)(0 - 9). The Regular Expression looks like this: "[1,2](Nothing here??):[0-5]\d". Here's what I need to do. If the first Digit is a "1", then the 2nd digit can be "\d" or "[0-9]" (for hours 0 - 19) . Otherwise, if the first digit is "2", then the 2nd digit can only be "[0-3]" (for hours to 20 - 23). Can anyone help?
-
Ok, I am trying to make an Expression to evaluate a Short Time format (hh:mm). I've seen the standard format of "\d\d:\d\d" but I can't use it because it allows values greater than 23 for the Hour. I've come up with one but I need help on the 2nd digit in the hour. Basically in laymen's terms it goes something like this: (1 or 2)(??):(0 - 5)(0 - 9). The Regular Expression looks like this: "[1,2](Nothing here??):[0-5]\d". Here's what I need to do. If the first Digit is a "1", then the 2nd digit can be "\d" or "[0-9]" (for hours 0 - 19) . Otherwise, if the first digit is "2", then the 2nd digit can only be "[0-3]" (for hours to 20 - 23). Can anyone help?
Try this:
^(?<hour>\d|([0-1]\d)|(2[0-3])):(?<min>[0-5]\d)$
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Ok, I am trying to make an Expression to evaluate a Short Time format (hh:mm). I've seen the standard format of "\d\d:\d\d" but I can't use it because it allows values greater than 23 for the Hour. I've come up with one but I need help on the 2nd digit in the hour. Basically in laymen's terms it goes something like this: (1 or 2)(??):(0 - 5)(0 - 9). The Regular Expression looks like this: "[1,2](Nothing here??):[0-5]\d". Here's what I need to do. If the first Digit is a "1", then the 2nd digit can be "\d" or "[0-9]" (for hours 0 - 19) . Otherwise, if the first digit is "2", then the 2nd digit can only be "[0-3]" (for hours to 20 - 23). Can anyone help?
If you really want to do all the error checking in the regular expression for Short Time format, try this: ^(([01]?\d)|(2[0-3])):[0-5]\d$ Or more explicitly: ^(([01]?[0-9])|([2][0-3])):([0-5][0-9])$ If you don't want to allow h:mm times, take out the ?.