我有一个文本Summary for rxd7865 November 13, 2012 to November 13, 2012
。
我只想November 13, 2012
从上面的文本中获取日期字符串。
我如何在 PHP 中使用正则表达式来做到这一点?
正则表达式:
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
使用preg_match_all,它使用PCRE 语法