代码点火器正则表达式以匹配时间不起作用


Codeigniter regex to match time not working

我正在尝试使用此正则表达式([01]?[0-9]|2[0-3]):00:00将 24 小时格式与 00 分和秒相匹配。

在代码点火器的表单验证规则中,我正在输入正则表达式

regex_match[([01]?[0-9]|2[0-3]):00:00] 

并不断得到

Message: preg_match() [function.preg-match]: No ending matching delimiter ‘)’ found

正则表达式在 php 中工作,但不通过 CI 正则表达式规则。我做错了什么?:-/

您可以使用回调来代替正则表达式匹配器。

像这样:

$this->form_validation->set_rules('number', 'Number', 'callback_validate_24hourtimestamp');
function validate_24hourtimestamp ($num)
{
    return preg_match('/^([01]?[0-9]|2[0-3]):00:00$/', $num);
}