Try this: #!/usr/bin/perl for (<>) { s!\{_(Start|End)Range_\}!!; while (s!\{_Index\s+([^}]+)\s+_\}!!) { print "%s/$1//g\n" } } HTH, Alex.