use strict; my $chr; $chr = "this and that"; $chr =~ s/ (?: (this|that)(?{print "matched lh alt: $1 "}) # Left hand alternate | (\w+)(?{print "matched rh alt: $2 "}) # Right hand alternate ) (?{print 'def 1' if defined ($1); print 'def 2' if defined ($2); print "\n"}) /$1\U$2/xg; print "$chr\n";