use Regexp::NamedCaptures; $_ = "three - four - five"; /(\w+)\C{baz} - (\w+)\C{qux}/g; print "baz=$baz, qux=$qux\n"; #### package Regexp::NamedCaptures; use overload; sub import { shift; die "No argument allowed to " . __PACKAGE__ . "::import" if @_; overload::constant qr => \ &convert; } sub convert { my $re = shift; $re =~ s( \\ ( \\ | C\{ (?>\s*) ((?>\w+)) (?>\s*) \} ) ) { defined $2 ? "(?{\$$2=\$^N})" : "\\" }xeg; $re; } 1;