Regex Date and Time?
I am trying to set up a date validation using regex to match the date and time format:
dd/mm/yy:HH:MM
This is what I have so far:
("([0-2]{0,1}[0-9]|30|31)/([01]{0,1}[0-9])/([0-9]{2}):([0-9]{2}):([0-9]{2})")
As you can see, there is nothing to stop the months going above 12, the hours going above 24, and the minutes above 59.
Can someone please show me how to do this? :)
[419 byte] By [
hshah] at [2007-11-11 7:59:10]

# 1 Re: Regex Date and Time?
http://www.regexlib.com/
Michael Ash is a regex god!!!
# 2 Re: Regex Date and Time?
You can download JMask from http://www.javawisdom.com/
Now it's free and opne source
JMask is a 100% Pure JAVA Component especially for JAVA developers who need to construct a date ,time ,alphabet ,numeral and alpha-numeral inputting mask for JAVA application and applet
# 3 Re: Regex Date and Time?
http://www.regexlib.com/
Michael Ash is a regex god!!!
http://www.regexlib.com/REDetails.aspx?regexp_id=488
Trying to get that working in Java, but it doesn't seem to like it!
hshah at 2007-11-11 22:38:54 >

# 4 Re: Regex Date and Time?
try this one.
(0{0,1}[1-9])|(1/d)|(2/d)|(3[0-1])/(0{0,1}[1-9])|(1[0-2])/([1-9]/d):(0{0,1}/d)|(1/d)|(2[0-4]):(0{0,1}/d)|([1-5]/d)
/d stands for digit =[0-9]
this should work fine, but has no checkings about 30. february.
another way just would be to parse the user input to a date through simpledateformat.
then you know, if it's a real existing date.
# 5 Re: Regex Date and Time?
try this one.
(0{0,1}[1-9])|(1/d)|(2/d)|(3[0-1])/(0{0,1}[1-9])|(1[0-2])/([1-9]/d):(0{0,1}/d)|(1/d)|(2[0-4]):(0{0,1}/d)|([1-5]/d)
/d stands for digit =[0-9]
this should work fine, but has no checkings about 30. february.
another way just would be to parse the user input to a date through simpledateformat.
then you know, if it's a real existing date.
I tried that one without the time part to it, and it just doesn't seem to work. It rejects everything I put into it...
hshah at 2007-11-11 22:40:52 >

# 6 Re: Regex Date and Time?
that was just a suggestion,
of course the correct syntax for regular expressions has to be used(\\d instead of /d). also exact grouping has to be considered.
the correct code is like this (only the date part):
(the ^ enshures, that the matching begins at the start of the given text, within the text, the ? stands for {0,1}, also see the javadoc for Pattern class)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegex {
public static void main(String[] args) {
Pattern pat = Pattern.compile("^((0?[1-9])|(1\\d)|(2\\d)|(3[0-1]))\\/((0?[1-9])|(1[0-2]))\\/(\\d\\d)");
String tests [] = {
"11/11/06",
"11/12/06",
"11/13/06",
"abcdef",
"32/11/06"
};
Matcher mat;
for (int i=0; i<tests.length; i++){
mat = pat.matcher(tests[i]);
System.out.print("input: "+tests[i]+" ");
if (mat.find())
System.out.println("matched with: "+mat.group());
else
System.out.println("didn't match.");
}
}
}
results in:
input: 11/11/06 matched with: 11/11/06
input: 11/12/06 matched with: 11/12/06
input: 11/13/06 didn't match.
input: abcdef didn't match.
input: 32/11/06 didn't match.
