my $test = qq|Some test. This is a test-with a dash. We want to match test here|; $test =~ s{\b(test)\b}{change_for_url($1)}eg; sub change_for_url { print "GOT: '$_[0]'\n"; } #### GOT: 'test' GOT: 'test' GOT: 'test'