[英]match/omit multiple lines in php preg regex
currently have a regex in a PHP preg_match statement preg_match($regex,trim($searchText),$matches);
当前在PHP preg_match语句中有一个正则表达式
preg_match($regex,trim($searchText),$matches);
The regex being used is (without delimiters) 使用的正则表达式为(不带分隔符)
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+)
that runs against the following $searchText
just fine (as expected) 对下面的
$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
returning the various named elements in the $matches array. 返回$ matches数组中的各种命名元素。 However we have had a new element introduced (flights) which may have 1 or more lines both in departure and return.
但是,我们引入了一个新元素(航班),该元素在出发和返回时可能有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
Running against some hurdles getting to capture (and/or discard) the variable flight information line(s) that may appear, while leaving the rest of the matching/returning intact. 克服一些障碍,以捕获(和/或丢弃)可能出现的可变飞行信息行,而其余匹配/返回信息保持不变。
Thanks in advance. 提前致谢。
Not sure why .
不知道为什么
.
isn't working for you, but [\\s\\S]*
(or ([\\s\\S]*)
for capture) should work to grab the flight chunk: 不适用于您,但是
[\\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";
?>
result: 结果:
-> 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.