in reply to Re: How to remove everything after last occurrence of a string?
in thread How to remove everything after last occurrence of a string?
Try tybalt's code with:
As far as maintainability and understandability goes, I would not use the \K and prefer the more common way:$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+s +qlite-3.10.0
I think \K is specific to Perl or at least I know that it does not exist in some other regex dialects that I use.instead of : $path =~ s/.*\Q$version\E\K.*//s; I probably would have coded: $path =~ s/(.*\Q$version\E).*/$1/s;
From the use case presented, the speed of execution doesn't matter at all. I would opt for simplicity and avoid uncommon things like \K. I could code a faster, better version of your index() approach in ASM and it would run like a "super rocket" but to absolutely no effect whatsoever upon total program execution time. And I think this could miss use cases involving wide characters which the regex will handle as part of Perl (the one byte per character assumption although extremely useful for many things, it does have some limitations).
I don't know why the /s regex modifier is used and the rationale behind that could be a bit obscure? Normally "." matches anything except \n. /s allows "." to include the "\n". I would not expect to see an \n in a path name. I'm not sure that this makes any difference at all, but again, some of these small things can matter depending upon the circumstances.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to remove everything after last occurrence of a string?
by AnomalousMonk (Archbishop) on Jun 07, 2022 at 00:58 UTC | |
by Marshall (Canon) on Jun 07, 2022 at 01:27 UTC | |
|
Re^3: How to remove everything after last occurrence of a string?
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 |