s/([a-m])([n-z])/$1 . myfunction($1, $2) . $2/eg; sub myfunction { my ($pre, $post) = @_; # we could do something smart with the pre- and post- # strings, but instead we'll just return a 5. return 5; }