#!/usr/bin/perl -wl use strict; my $str = '{fred and barney} {bam bam} {pebbles} {} { }'; print $str; $str =~ s|({[^}]+})|(my $tmp = $1) =~ s/ /_/g; $tmp|eg; print $str; __END__ Prints: {fred and barney} {bam bam} {pebbles} {} { } {fred_and_barney} {bam_bam} {pebbles} {} {___} #### my $str2 = '{fred and barney} {bam bam} {pebbles} {} { }'; print $str2; my $var = "xxxxx"; $str2 =~ s|({[^}]+})|{$var}|g; print $str2; __END__ Prints: {fred and barney} {bam bam} {pebbles} {} { } {xxxxx} {xxxxx} {xxxxx} {} {xxxxx}