Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Regular expression

0 views
Skip to first unread message

JustWondering

unread,
Dec 23, 2009, 9:07:39 AM12/23/09
to
Code:
<?php

$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?

matt

unread,
Dec 23, 2009, 9:30:21 AM12/23/09
to

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;
}


JustWondering

unread,
Dec 23, 2009, 10:41:58 AM12/23/09
to

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?

Peter H. Coffin

unread,
Dec 23, 2009, 11:10:42 AM12/23/09
to


'\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

Taras Kudla

unread,
Dec 23, 2009, 3:15:31 PM12/23/09
to
try $regexp = "/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})$/";
Message has been deleted
0 new messages