my $str = "//111.11.112.12/abc/main/myfolder/docs"; my $replace = "something"; $str =~ s#.+(?=/main)#$replace#"; print "str now = $str";