2012-08-23 65 views
0

我有這個字符串需要使用pregmatch

配套齊全的自包含的,以從字符串中提取日期2間臥室的套房僅5分鐘步行至UVIC適用於9月1日

現在我使用pregmatch提取它:這是正則表達式。

'/\bavailable\\s(?P<date_available>[?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|immediately]+[\\s\d]+)[st|nd|rd|th]?/i' 

目前這個表達式可以從字符串中提取:

Available september 1st. 
Available September 2nd 
available september 3rd 
available september 4th 
available sept 1 

輸出的例子是:

Array 
(
    [0] => available September 1 
    [date_available] => September 1 
    [1] => September 1 
) 

但我不能找到一種方法,當琴絃提取:

Available for september 1st. 
Available in September 2nd 
available since september 3rd 
available at september 4th 

任何人可以幫我處理這個問題嗎?感謝

+0

難道你不能只改變'available'到'available \ s +(for | in | at | since |)\ s *'? – h2ooooooo

+0

感謝您的想法。 。 ;) –

回答

1

通配符AZ,2-5字母(相匹配,如 「上」 的東西):

$regex = '/\bavailable[ ]*(?:[a-z]{2,5})?[ ]*' . 
    '(?P<date_available>immediately|now|' . 
    '(?:(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?' . 
    '|Apr(?:il)?|May|Jun(?:e)|Jul(?:y)?|Aug(?:ust)?' . 
    '|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)' . 
    '[ ]+[\d]+))' . 
    //end <date_available> 
    '(?:st|nd|rd|th)?/i'; 

用法:

$lines = array(
    'Fully furnished self contained 2 bedroom suite just 5 minute walk to UVIC is available now.', 
    'bedroom suite just 5 minute walk to UVIC is available on September 34.', 
    'bedroom suite just 5 minute walk to somewhere is available on Apr 1.', 
    ); 

foreach ($lines as $line) { 
    echo $line, "\n<br>\n"; 
    if (preg_match($regex, $line, $matches) === 1) { 
     print_r($matches['date_available']); 
    } else { 
     echo "Does not match."; 
    } 
    echo "\n<br>\n"; 
} 
+0

如果沒問題,你可以寫完整的正則表達式嗎?只需使用我提供的正則表達式謝謝 –

+0

這個正則表達式似乎也很好。如何改進它以支持: 立即可用,現在可用?謝謝 –

+0

現在好了嗎? – EthanB

0

與您的所有示例下面的作品,雖然我的天堂因爲我不知道它們的確切語法

\bavailable\s+(?:(?:for|in|at|since)\s+)?((?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Sept(?:ember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\s+\d{1,2}(?:st|nd|rd|th)?) 
+0

謝謝。這裏是我的最終正則表達式與「立即可用/現在」一起工作。 「/ \ bavailable \ S +(對於|在|在|之後|)\\ S *(P [?:一月(:uary)| 2月(:??????ruary)| 3月(?: CH)|四月(?:?IL)|五月|六月(?:E)|七月(?:?Y)|八月(:UST)|九月(:?????tember)|十月(:奧伯?)|十一月(?:?餘燼)| DEC(:?餘燼)|立即|現在|至] + [\\小號\ d] +)[ST | ND | RD |日]?/我' –

+0

哦,是的,完全忘了添加到底。 – HuggieRich

0

我實際上無法讓你工作在所有情況下,它看起來好像您試圖使用帶有方括號[ ]的字符類,而不是使用括號()的括號和交替。

以下是可能,我可以把它根據您的要求在最短的

$pattern = '/\bavailable\s+(?:(?:for|in|at|since)\s+)?((?:immediately|now)|(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Oct(?:ober)?|(?:Sept|Nov|Dec)(?:ember)?)\s+?\d{1,2}(?:st|nd|rd|th)?)/i'; 

這不包括已命名的子圖案,所需要的比賽中始終處於$matches[1]但是如果你想要包括一個名爲子模式,那麼你可以隨時把1英寸

$pattern = '/\bavailable\s+(?:(?:for|in|at|since)\s+)?(?P<date_available>(?:immediately|now)|(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Oct(?:ober)?|(?:Sept|Nov|Dec)(?:ember)?)\s+?\d{1,2}(?:st|nd|rd|th)?)/i'; 

針對@EthanB儘早解決,你似乎沒有被捕獲序後綴爲日期st, nd, rd, th,如果是這樣的話,它不是要求th如果不包括這一點,你可以縮短它的長度,但在日期編號之後嘗試匹配任何內容沒有任何意義。