在php preg regex中匹配/省略多行

时间:2013-01-07 19:10:05

标签: php 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数组中的各种命名元素。然而,我们引入了一个新元素(航班),在出发和返回时可能有一条或多条线路。

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

针对某些障碍运行,捕获(和/或丢弃)可能出现的可变航班信息行,同时保持匹配/返回的其余部分完好无损。

提前致谢。

1 个答案:

答案 0 :(得分: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, 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