http://qs1969.pair.com?node_id=11144509


in reply to Re^2: How to remove everything after last occurrence of a string?
in thread How to remove everything after last occurrence of a string?

G'day GrandFather,

++ Thanks for your input.

I did possibly end up (implicitly) suggesting that speed was the all-important factor.

I have seen innumerable cases where regexes have been used to test exact matches (/^some_string$/), test if strings start with some token (/^some_token/), and so on. As I said originally, "Perl's string handling functions (and operators) are, in my experience, substantially faster than achieving the same functionality with regexes."; as such, reaching for a regex first has become something of an annoyance for me.

The OP had asked "How it can be acheived without escapeing the special chars?" and I rather thought that was implicit in my "rindex" code. Perhaps I should have highlighted that.

I also answered the OP's title question, "How to remove everything after last occurrence of a string?". Again, I didn't highlight that.

I hadn't really considered the maintainability aspect but, I agree, the "rindex" code is easily understandable and works in all versions of Perl5; that's not to say that I shy away from regexes (see "Syntax-highlight Non-Perl Code for HTML"). Furthermore, if those maintaining the code are expected to have a solid grounding in regexes, then I'd say that neither solution is particularly complex and both are equally maintainable (a YMMV situation).

The OP may have a very specific reason for choosing a regex solution; however, if not, why not choose an alternative that's three times faster.

— Ken