in reply to How to remove everything after last occurrence of a string?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to remove everything after last occurrence of a string?
by Marshall (Canon) on Jun 06, 2022 at 22:39 UTC | |
by AnomalousMonk (Archbishop) on Jun 07, 2022 at 00:58 UTC | |
by Marshall (Canon) on Jun 07, 2022 at 01:27 UTC | |
by hv (Prior) on Jun 07, 2022 at 00:43 UTC | |
by Marshall (Canon) on Jun 07, 2022 at 01:10 UTC | |
by pryrt (Abbot) on Jun 07, 2022 at 14:09 UTC |