my $regex = 's/(\w+)\s+tacos/$1 yummy tacos/g'; $regex =~ s/(\\s\+)(\w)/$1(<\\w\\w>)$2/g; # Handle first part of substitution my $match = $2; $regex =~ s/ $match/\$2$match/g; # Handle second part of substitution eval "$regex";