[英]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.