2013-01-07 103 views
1

目前在一個PHP的preg_match聲明preg_match($regex,trim($searchText),$matches);匹配/忽略在PHP預浸正則表達式多行

正在使用的正則表達式是一個正則表達式(無定界符)

Primary Redeemer: (?<name>.*), (?<phone>.*), (?<email>.*).*[.\r\n\s]*.*Valid Travelers:.*[.\r\n\s]*.*Valid Days: (?<date_in>\d{4}\/\d{2}\/\d{2}) - (?<date_out>\d{4}\/\d{2}\/\d{2}).*[.\r\n\s]*.*Item: (?<desc>.*) \/.*[.\r\n\s]*.*Voucher #: (?<voucher>\d+) Itin. #:(?<itin>\d+) 

,對以下$searchText就好運行(與預期的一樣)

Booking 
1 Travelers -- Vehicles: 1 (TBA), 
    Primary Redeemer: Joe Schmoe, 1 (555) 5555555, [email protected] 
    Valid Travelers: Joe Schmoe, Sue Schmoe, Schmoe twins, 
    Valid Days: 2012/01/01 - 2012/02/02 
    Item: Some Item Purchased - weekly 12345/
    Voucher #: 10835756 Itin. #:153244150897 

返回$ matches數組中的各種命名元素。然而,我們已經引入了一個新的元素(航班),這些元素在出發和返回時可能有一個或多個線路。

Booking 
1 Travelers -- Vehicles: 1 (TBA), 
    Primary Redeemer: Joe Schmoe, 1 (555) 5555555, [email protected] 
    Valid Travelers: Joe Schmoe, Sue Schmoe, Schmoe twins, 
    Valid Days: 2012/01/01 - 2012/02/02 
    Flight ABC to DEF 
     AL 1234 departs ABC 01/01/2012 06:15 arrives BCD 01/01/2012 08:45 
     AL 2345 departs BCD 01/01/2012 09:40 arrives DEF 01/01/2012 11:33 
    Flight DEF to ABC 
     AL 3456 departs DEF 02/02/2012 10:50 arrives BCD 02/02/2012 13:12 
     AL 4567 departs BCD 02/02/2012 14:00 arrives ABC 02/02/2012 15:30 
    Item: Some Item Purchased - weekly 12345/
    Voucher #: 10835756 Itin. #:153244150897 

運行鍼對一些障礙越來越捕捉(和/或丟棄)可能出現的變量航班信息線(S),同時保持匹配的休息/恢復完好。

在此先感謝。

+1

使用多個正則表達式的目標在不同的信息片段可能是最好的方法。 – datasage

+0

我一直認爲它是*喬Shmo *,huh – Madbreaks

+0

@datasage - 多個正則表達式肯定是可行的,只是尋找雄辯的解決方案,如果它在那裏。 – Dave

回答

1

不知道爲什麼.沒有爲你工作,但[\s\S]*(或([\s\S]*)捕獲)應努力抓住飛行塊:

<?php 

    $regex = "/Primary Redeemer: (?<name>.*), (?<phone>.*), (?<email>.*).*[.\r\n\s]*.*Valid Travelers:.*[.\r\n\s]*.*Valid Days: (?<date_in>\d{4}\/\d{2}\/\d{2}) - (?<date_out>\d{4}\/\d{2}\/\d{2}).*[.\r\n\s]*[\s\S]*Item: (?<desc>.*) \/.*[.\r\n\s]*.*Voucher #: (?<voucher>\d+) Itin. #:(?<itin>\d+)/"; 

    $searchText = <<<SEARCHTEXT_HEREDOC 
Booking 
1 Travelers -- Vehicles: 1 (TBA), 
    Primary Redeemer: Joe Schmoe, 1 (555) 5555555, [email protected] 
    Valid Travelers: Joe Schmoe, Sue Schmoe, Schmoe twins, 
    Valid Days: 2012/01/01 - 2012/02/02 
    Flight ABC to DEF 
     AL 1234 departs ABC 01/01/2012 06:15 arrives BCD 01/01/2012 08:45 
     AL 2345 departs BCD 01/01/2012 09:40 arrives DEF 01/01/2012 11:33 
    Flight DEF to ABC 
     AL 3456 departs DEF 02/02/2012 10:50 arrives BCD 02/02/2012 13:12 
     AL 4567 departs BCD 02/02/2012 14:00 arrives ABC 02/02/2012 15:30 
    Item: Some Item Purchased - weekly 12345/
    Voucher #: 10835756 Itin. #:153244150897 
SEARCHTEXT_HEREDOC; 

    preg_match($regex,trim($searchText),$matches); 

    echo "\n"; 
    foreach($matches as $match) { 
    echo " -> ".$match; 
    echo "\n"; 
    } 
    echo "\n"; 
?> 

結果:

-> Primary Redeemer: Joe Schmoe, 1 (555) 5555555, [email protected] 
    Valid Travelers: Joe Schmoe, Sue Schmoe, Schmoe twins, 
    Valid Days: 2012/01/01 - 2012/02/02 
    Flight ABC to DEF 
     AL 1234 departs ABC 01/01/2012 06:15 arrives BCD 01/01/2012 08:45 
     AL 2345 departs BCD 01/01/2012 09:40 arrives DEF 01/01/2012 11:33 
    Flight DEF to ABC 
     AL 3456 departs DEF 02/02/2012 10:50 arrives BCD 02/02/2012 13:12 
     AL 4567 departs BCD 02/02/2012 14:00 arrives ABC 02/02/2012 15:30 
    Item: Some Item Purchased - weekly 12345/
    Voucher #: 10835756 Itin. #:153244150897 
    -> Joe Schmoe 
    -> Joe Schmoe 
    -> 1 (555) 5555555 
    -> 1 (555) 5555555 
    -> [email protected] 
    -> [email protected] 
    -> 2012/01/01 
    -> 2012/01/01 
    -> 2012/02/02 
    -> 2012/02/02 
    -> Some Item Purchased - weekly 12345 
    -> Some Item Purchased - weekly 12345 
    -> 10835756 
    -> 10835756 
    -> 153244150897 
    -> 153244150897 
+0

我不知道它爲什麼不起作用,但\ s \ s伎倆,謝謝。 – Dave

+0

FWIW,這裏是描述dotall和多行修飾符的php修飾符文檔:[http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php](http://www.php達網絡/手動/ EN/reference.pcre.pattern.modifiers.php) – mgamba