use String::Approx 'amatch'; use re 'eval'; use re 'debug'; $r = qr/((?:@{[ ramatch( 'APT' ) ]}\s)?\w+\d)$/; $apt = ( "5678 DELAWARE AVENUE AOT 123" =~ $r )[0]; $\ = "\n"; print "\$apt=$apt"; sub ramatch { my $match = shift; my $match_len = length $match; $match =~ s(\\)(\\\\)g; $match =~ s(')(\\')g; return "(?x:\Q$match\E | (?(?{scalar amatch('$match',substr(\$_,pos(),$match_len))}) (?s:@{[ q[.] x $match_len]}) | (?!)))"; }