Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^3: How to remove everything after last occurrence of a string?

by kcott (Archbishop)
on Jun 08, 2022 at 08:47 UTC ( #11144509=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144509]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-08-10 02:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found