如何使用正则表达式从文本中获取日期

时间:2013-01-22 10:24:00

标签: php regex

我有一个文字Summary for rxd7865 November 13, 2012 to November 13, 2012

我想从上面的文字中仅提取November 13, 2012之类的日期字符串。

如何在PHP中使用正则表达式来执行此操作?

2 个答案:

答案 0 :(得分:2)

正则表达式:

preg_match('/(?<the_date>(January|February|March) [0-9]{2}, 20[0-9]{2})/', $string, $matches);
echo $matches[the_date];

解释:

() // are called capture groups or matches
(?<name>) // are named capture groups or named matches
| // separator between a list of alternative matches
[] // is a character class
[0-9] // is a character class that allows only characters from 0 to 9
{} // is a repetition specifier
[]{2} // allows the character class to repeat twice

答案 1 :(得分:0)

使用preg_match_all,它使用PCRE syntax