use strict; use warnings; my $line = 'OPENINFO="Oracle_XA:Oracle_XA+Acc=P/NASIT/nasit+SesTm=60+SqlNet=QANASIT+LogDir=/users/nas/disk1/data/traces+DB=QANASIT+Objects=true+Threads=true"'; my $DBUser = '$DBUser'; my $DBPass = '$DBPass'; my $DBSid = '$DBSid'; my $openinfo_rx = qr/ \A OPENINFO=" # openinfo line start ( [^"]+ ) # whatever inside the '"'s " \z # openinfo line end /x; if ($line =~ m/$openinfo_rx/) { my $openinfo_content = $1; my $quoted_openinfo_content = quotemeta( $1 ); my (@openinfo_parts) = split( /$quoted_openinfo_content/, $line ); $openinfo_content =~ s{ =P/ # =P/ starts user/pass part ([^+]+) # whatever except + \+ # end mark for user/pass part } {=P/$DBUser/$DBPass+}x; $openinfo_content =~ s{ \+(SqlNet|DB)= # ([^+]+) # whatever except + \+ # end mark } {+$1=$DBSid+}xg; $line = join( $openinfo_content, @openinfo_parts); } print $line;