my $string = "Example 1: 413-577-1234 Example 2: 981-413-777-8888 Example 3: 413.233.2343 Example 4: 562-3113 Example 5: 401 311 7898 Example 6: 2342343-23878-878-2343 Example 7: 1 (413) 555-2378 Example 8: 4135552378 Example 9: 413 789 8798 343 9878"; extract($string); sub extract { my %results = (); my $string = shift; # pad string with spaces to make it easier to find phone numbers at beginning and end of strings $string = ' ' . $string . ' '; # get rid of consecutive whitespace characters to make regex easier and faster $string =~ s/(\s){2,}/$1/g; # find patterns in the string that look like phone numbers my @matches = $string =~ / # Look for ten digit North American numbers (?:1(?:\.|\s|-))* # optional 1 followed by period OR whitespace or dash \(? # optional opening paren \d{3} # three consecutive digits (?:\.|\)|-|\s|\)\s)? # optional punctuation (period, close paren, dash, whitespace) \d{3} # three consecutive digits (?:\.|-|\s)? # optional punctuation \d{4} # 4 consecutive digts | # Look for seven digit North American numbers \d{3} # three consecutive digits (?:\.|-|\s)? # optional punctuation \d{4} # 4 consecutive digts /gx; say "Match: '$_'" for @matches; }