use strict; use warnings; my $re; $re = qr/ (\d) (?{ print("[$1]\n") }) /x; 'thr33' =~ $re; __END__ output ====== [3]