my $jdsc = qr/(?:national\s+security\s+adviser\s+)/; my $title = qr/(?:dr.|doctor)\s+/; my $fn = qr/(?:condoleeza\s+)/; my $sn = qr/rice/; my $repl = 'condoleezarice'; while(){ s{ ( ( $jdsc? $title? $fn? ) $sn ) } { $2 ? $repl : $1 }exig; print; } __DATA__ white rice dr. rice doctor rice dr. condoleeza rice doctor condoleeza rice condoleeza rice national security adviser doctor condoleeza rice