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"; #### matched lh alt: this def 1 matched rh alt: and def 2 matched lh alt: that def 1 this AND that