如何将ereg()表达式转换为preg_match()?

时间:2011-06-23 21:06:56

标签: php regex

  

可能重复:
  Converting ereg expressions to preg

我有这个ereg()表达式:

^[0-9]{8}\T{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}$

如何将其转换为preg_match?

2 个答案:

答案 0 :(得分:3)

表达式看起来很好,但你可以简化它:

^\d{8}\t\d{2}:\d{2}:\d{2}$

您的原始版本仍应使用preg,但上面的内容更易于阅读和理解。几点说明:

  • [0-9]\d
  • 相同
  • {1}是不必要的
  • :不需要转义

答案 1 :(得分:2)

你走了。

$subject = 'the string i want to search through';
$pattern = '/^[0-9]{8}\T{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}$/';
$matches = array();
preg_match($pattern, $subject, $matches);

print_r($matches);