$do_the_change_mon = sub { $thingy = shift; $thingy =~ tr|/|_|; return $thingy; } $expr = s| \[([A-Z/]+)\s([A-Z/]+)\s([A-Z/]+)\s([A-Z/]+)\s\] |&$do_the_change_mon("$1_$2_$3_$4)|xe;