PHP regex needed for dd/mm/yyyy format

You could capture the separator in a group and then use a back reference to match what was captured.

For example, ([ab])c\\1 will match aca and bca, but not acb or bca.

Do you want to allow any trailing characters after the date? (i.e. there’s nothing anchoring the pattern at the end)

If you’re asking me, then no.

So perhaps a back reference to capture whether they used ./- for the first separator, then require it for the second?

Was painful! But I got it nonetheless, for anyone whose interested:


/**
 * Matches dates with period, hyphen, or slash as the seperater
 * however they have to be the same.
 * I.E. 10.5-1975 won't return true, however 10.5.1975 OR 10-5-1975 will
 **/
function isDate($date)
{
	return (preg_match("~^\\d{1,2}([/.-])\\d{2}\\\\1\\d{4}$~", $date)==1) ? TRUE : FALSE;
}

Not extremely useful, just more of a backreferencing test for me? :-p

No, sorry. 't was a typo. :slight_smile: