There are many possible approaches to this. One is to quotemeta() only inside the pattern:
$version = "3.5.2+tcl-tk-8.5.18+sqlite-3.10.0"; $path =~ s/\Q$version\E.*/$version/s;
Another is to replace with what the pattern actually matched:
$version = quotemeta("3.5.2+tcl-tk-8.5.18+sqlite-3.10.0"); $path =~ s/($version).*/$1/s;
Another is to use index() to search for a fixed string, rather than a regexp:
$version = "3.5.2+tcl-tk-8.5.18+sqlite-3.10.0"; my $pos = index($path, $version); # cut everything beyond the match, if there was a match substr($path, $pos + length($version)) = '' if $pos >= 0;
My guess is that tybalt89's solution with \K will be the fastest, but not necessarily the easiest to understand and maintain.
In reply to Re: How to remove everything after last occurrence of a string?
by hv
in thread How to remove everything after last occurrence of a string?
by ovedpo15
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |