$a = "4 birds, 11 cats, 6 dogs, 9 pigs, 3 cats, 20 dogs";

$a =~ s/\d+ (cats|dogs)/3 $1/g; # use () for alternatives and also for storing!

print "$a";
