#! perl use strict; use warnings; use Data::Dump; while () { chomp; # Trim trailing newline my @apts = /[- \d]+/g; # Get all sequences of digits, spaces, & hyphens s/^[ ]+ //x for @apts; # Trim initial spaces s/ [ ]+$//x for @apts; # Trim trailing spaces @apts = grep { /^\d{3,4}$/ } @apts; # Get 3- or 4-digit sequences only dd \@apts; } __DATA__ John Smith lives in Apts. 123 & 456, home number: 555-6666-7777 Please phone Jane Doe on 111 2222 3333. She lives in apartment 789 in the Main building. Please phone Janet Roe on 88899990123. She lives in apartment 100 in the Main building.