$regexp='\b(0?[1-9]|1[012])[/](0?[1-9]|[12][0-9]|3[01])[/](19|20)?[0-9]
{2}\b';
$text="12/23/2009";
do_reg($text, $regexp);
function do_reg($text, $regex)
{
if (preg_match($regex, $text)) {
echo "Match";
}
else {
echo "No Match";
}
}
?>
Output:
Warning: preg_match() [function.preg-match]: Delimiter must not be
alphanumeric or backslash in C:\xampp\htdocs\test\EmptyPHP.php on line
12
No Match
This is supposed to give me a match for a mm/dd/yyyy date. Any ideas?
or an alternate regex for that?
Well, like the error message says...you have a delimiter problem.
(You don't have one) Since you're using forward slashes in your
regex, you may choose to use something like '?' as a delimiter.
However, if you are simply trying to validate a date, there are much
easier ways!
// tests date for validity and returns in mm/dd/yyyy
// format or false on failure
function testDate($date)
{
$timestamp = strtotime($date);
return ($timestamp !== false) ? date("m/d/Y", $timestamp) : false;
}
I am using the test program to test the regex' validity. The final
thing is going somewhere else, and a regex is required. What do you
mean by I don't have a delimiter?
'\b1234\b' is not a regular expression match pattern.
'/\b1234\b/' is a regular expression match pattern. The delimiter (in
this case, / ) is not optional.
--
Life does not cease to be funny when people die any more than it ceases
to be serious when people laugh.
-- George Bernard Shaw