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]}) | (?!)))"; } #### # This version wasn't very nice so I replaced it and commented it out. #use String::Approx 'amatch'; #use re 'eval'; # #$r = qr/(@{[ ramatch( 'APT' ) ]}\s\w+\d)$/; #$apt = ( "5678 DELAWARE AVENUE APT 123" =~ $r )[0]; # #$\ = "\n"; #print "\$apt=$apt"; # #sub ramatch { # my $match = shift; # my $match_len = length $match; # # escape $match # our $ONCE = 0; # return # "(?x:(?(?{\$ONCE += amatch('$match',substr(\$_,pos(),$match_len)); # \$ONCE>1}) # (?s:@{[ q[.] x $match_len]}) | # (?!)))"; #}