Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Marshall (Canon)
on Jun 07, 2022 at 01:10 UTC ( [id://11144467]=note: print w/replies, xml ) Need Help??


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

Glad we have a common understanding of what the code examples actually do.

I was just looking at the node title: "How to remove everything after last occurrence of a string?". I suspect that is more restrictive than what the OP actually needs (i.e. I suspect that first or last doesn't matter).

As far as \K goes, this to me, in this case, is some "Perl Candy". The same functionality is easily done without it. Many regex implementations do not have this, so in terms of "easy to understand", it flunks unless you are a Perl'er who uses this feature often.

  • Comment on Re^4: How to remove everything after last occurrence of a string?

Replies are listed 'Best First'.
Re^5: How to remove everything after last occurrence of a string?
by pryrt (Abbot) on Jun 07, 2022 at 14:09 UTC
    As far as \K goes, this to me, in this case, is some "Perl Candy". ... Many regex implementations do not have this, so in terms of "easy to understand", it flunks unless you are a Perl'er who uses this feature often.

    The only other regex library I regularly use, other than Perl's, is the Boost library used in Notepad++ for regex search/replace. The Boost library documentation has mentioned \K since v1.40.0, which was released in 2009, so nearly 13 years ago. The oldest Notepad++ you can easily download from their website is v6.2.3 from 2012, and it allows \K -- so every Notepad++ download for the last decade has included this feature that you seem to think is "rare" and "Perl Candy".

    If you type \K into regex101.com, it will tell you in the explanation for both "PCRE2 (PHP>=7.3)" and "PCRE (PHP <7.3)" that it means it resets the match, though the other flavors there don't seem to know it. The site rexegg.com shows \K on both Quick Start: Cheat Sheet and their Best/Greatest Regex Trick Ever page

    The site regular-expressions.info has a whole page on "keep" and allows you to compare the functionality of \K between various regex engines on the Regular Expression Reference: Special Groups page. Based on selecting each of their options (other than Perl and Boost) from the dropdown, it looks like JGsoft has had it since V2, .NET doesn't, Java doesn't, PCRE since 7.2, PCRE2 with no limit, PHP since 5.2.4 (which PHP releases says was from 2007), Delphi has it, R has it, JavaScript does not, VBScript does not, VRegExp does not, Python does not, Ruby does since 2.0, std::regex does not, and the rest they list do not have that feature. So, while it's by no means all regex engines that have it, many do, including ones shipped with languages like PHP, Delphi, R, and Ruby. So I'd say \K is not just "Perl Candy" by any stretch of the imagination.

    Personally, \K is easier to understand to me than more complicated lookbehind syntax (which is available in many more of those regex flavors mentioned above), and has the power of being variable-width, which most of the normal lookbehinds (including Perl's) is not.

    You are, of course, allowed to not use it if you don't want. And I often default to recommending Notepad++ regex-newbies use a capture group and include the $1 rather than using \K because of Notepad++ quirks (where \K only works as expected with Replace All, not with multiple single Replace). And to warn others that it requires Perl v5.10 is sensible. But to avoid it just because it's "Perl Candy" is a hard-to-defend position, IMO.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found