[英]match/omit multiple lines in php preg regex
當前在PHP preg_match語句中有一個正則表達式preg_match($regex,trim($searchText),$matches);
使用的正則表達式為(不帶分隔符)
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, schmoe@joe.com
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數組中的各種命名元素。 但是,我們引入了一個新元素(航班),該元素在出發和返回時可能有1條或更多條線路。
Booking
1 Travelers -- Vehicles: 1 (TBA),
Primary Redeemer: Joe Schmoe, 1 (555) 5555555, schmoe@joe.com
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\\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, schmoe@joe.com
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, schmoe@joe.com
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
-> schmoe@joe.com
-> schmoe@joe.com
-> 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.