in reply to Phone Number Regular Expression

use strict; use warnings; my $area = 800; my $pre = 555; my $not = 9999; $_ = join '', <DATA>; print "$area-$pre-$1\n" while m/$area(?:\)?\s|\.|-)$pre[\s\.-]((?!9999 +)\d{4})/g; __DATA__ 1-800-555-9999 1-800-555-3456 (800) 555-3456 800 555 3456 800 555-3456 800.555.3456 800 555.5555
I think this covers all eventualities.

Replies are listed 'Best First'.
Re^2: Phone Number Regular Expression
by lev36 (Sexton) on Mar 16, 2006 at 21:46 UTC

    Elegant, thanks!

    What I'm doing is editing a bunch of files on a web server to reflect the new area code - so I had planned to read each line of the input file, make the replacement, and then save the new version (and a backup of the old version). Then I thought about the line-break issue.

    So I think Roy's solution of grabbing a couple lines together, checking them, and them splitting them again before writing to the new file is what I need to do, in order to catch numbers that split over two lines. I see some stuff in your example that can help me with that.