I'm parsing a file with address details. Sometimes the address has one fax number, sometimes it has two fax numbers (on separate lines). How can I say "If there's a second fax number, grab it. If not, forget it" to my regex engine?
Here's an example with one fax number:
Tel: +1 212 12345
Fax: +1 212 23456District HQ: Springfield
and with two fax numbers:
Tel: +1 212 12345
Fax: +1 212 23456
Fax: +1 212 34567District HQ: Springfield
I'm using PHP's preg_match_all
to get matches:
$shop_name_regexp =
'/' .
'Tel.*?:(?<telephone>.*?)\n' . // Tel: or Telephone:
'Fax:(?<fax>.*?)\n.*?' .
'/s';
preg_match_all($shop_name_regexp, $string, $hit);
I thought I could grab the second fax number with something like [Fax:(?<fax2>.*?)]?
but I can't get it working. Any help appreciated.