# works with both a keyed number and a track data my $card_num = $tender->ccnumber; $card_num =~ s/^.? # Start sentinel [bB]? # Format code="B" ( # Capture to $1 [^^]+ # Primary account number ) # End of card .* # Slurp up the rest /$1/x; $card_num =~ s/^(.+)(.{4})$/('*'x(length $1)).$2/e;