2013-01-22 18 views
1

我有一個文本Summary for rxd7865 November 13, 2012 to November 13, 2012如何使用正則表達式從文本中獲取日期

我想從上面的文本只提取日期字符串像November 13, 2012

我如何在PHP中使用正則表達式來做到這一點?

+0

你嘗試過什麼?我敢肯定它很容易谷歌如何在PHP中使用正則表達式...你有困難與正則表達式的某些特定部分? – pcalcao

+0

日期格式有多固定? –

+0

嘗試http://webcheatsheet.com/php/regular_expressions.php – Guy

回答

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 
+0

非常感謝您的回覆。 – user1829627

+0

只有它不起作用。你忘了正則表達式分隔符和'to ...'部分(OP當然可以自己弄清楚)。 –

+0

與正則表達式分隔符相得益彰。其餘的是OP **有**自己想出來的東西。例如,我也沒有包括所有12個可能的月份。 –

0

隨着preg_match_all,它採用PCRE syntax

+1

我*認爲*這不是OP所希望的答案...... :) –

+0

@TimPietzcker然後問題應該更具體; –

+0

@fab,這是一個非常具體的問題。懶惰但具體。 –

相關問題