1

目前在 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

在捕获(和/或丢弃)可能出现的可变飞行信息行时遇到一些障碍,同时保持匹配/返回的其余部分完好无损。

提前致谢。

4

1 回答 1

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
于 2013-01-07T21:17:39.570 回答