简体   繁体   中英

match/omit multiple lines in php preg regex

currently have a regex in a PHP preg_match statement 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)

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. However we have had a new element introduced (flights) which may have 1 or more lines both in departure and return.

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:

<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM