#!/usr/local/bin/perl -w use strict; while () { s /[\n\r]//g; print "Testing with $_, result is "; m/(?:1[- ]?)? # Testing for an leading 1 and dash or space behind it \(? # Optional parentheses around the area code (\d{3}) # Area code \)? # Close optional parentheses around area code [- ]? # Optional dash or space after area code (\d{3}) # Exchange [- ]? # Optional dash or space after exchange (\d{4}) # Line /x; # /x modifier allows whitespace in a regex my $areacode = $1; my $exchange = $2; my $line = $3; print "($areacode) $exchange-$line\n"; } __DATA__ 212-555-1212 (212)555-1213 1-(212)-555-1214 1-212-555-1215 212 555 1216 (212) 555 1217 1 (212) 555 1218 1 212 555 1219 12125551210