tags:

views:

45

answers:

1

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 23456

District HQ: Springfield

and with two fax numbers:

Tel: +1 212 12345
Fax: +1 212 23456
Fax: +1 212 34567

District 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.

+1  A: 

You've almost got it :)

$shop_name_regexp = 
    '/' . 
    'Tel.*?:(?<telephone>.*?)\n' . // Tel: or Telephone:
    'Fax:(?<fax>.*?)\n\s*?' .
    '(Fax:(?<fax2>.*?))?\n.*?' .
    '/s';
preg_match_all($shop_name_regexp, $string, $hit);

The square brackets indicate a character class. So to match a, b, or c you would use square brackets: [abc]. To create a optional group you use parens.

Josiah
Brilliant, works perfectly. Thanks very much.
nevan
Glad to help :)
Josiah