我有一個文本Summary for rxd7865 November 13, 2012 to November 13, 2012
。如何使用正則表達式從文本中獲取日期
我想從上面的文本只提取日期字符串像November 13, 2012
。
我如何在PHP中使用正則表達式來做到這一點?
我有一個文本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
非常感謝您的回覆。 – user1829627
只有它不起作用。你忘了正則表達式分隔符和'to ...'部分(OP當然可以自己弄清楚)。 –
與正則表達式分隔符相得益彰。其餘的是OP **有**自己想出來的東西。例如,我也沒有包括所有12個可能的月份。 –
我*認爲*這不是OP所希望的答案...... :) –
@TimPietzcker然後問題應該更具體; –
@fab,這是一個非常具體的問題。懶惰但具體。 –
你嘗試過什麼?我敢肯定它很容易谷歌如何在PHP中使用正則表達式...你有困難與正則表達式的某些特定部分? – pcalcao
日期格式有多固定? –
嘗試http://webcheatsheet.com/php/regular_expressions.php – Guy