On *nix: perl -pi -e 's{\b((?:[a-zA-Z0-9]+_)+[a-zA-Z0-9])+\(\)\b}{$1()}g' * On Windows (I think): perl -pi -e "s{\b((?:[a-zA-Z0-9]+_)+[a-zA-Z0-9]+\(\)\b}{$1()}g" * #### s{ # match and replace on $_ \b # start with a word boundary ( # capture the matches and store in $1 (?:[a-zA-Z0-9]+_)+ # letters and numbers followed by _ at least once [a-zA-Z0-9]+ # must end with letters/numbers ) \(\) # then () \b # followed by word boundary } { $1() # replace the matched text with this string } # substituting the value of $1 (the first capture) g # perform this replace on all matches in $_ #### abc abc() abc_ abc_() abc_def abc_def() abc_def_ abc_def_() abc_def_ghi abc_def_ghi() BECOMES: abc abc() abc_ abc_() abc_def() abc_def()() abc_def_ abc_def_() abc_def_ghi() abc_def_ghi()()