$path = "/some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0/a/b/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0/c"; # and you will see that only the last "/c" is deleted. /some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0/a/b/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0 #### instead of : $path =~ s/.*\Q$version\E\K.*//s; I probably would have coded: $path =~ s/(.*\Q$version\E).*/$1/s;