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

Re: strange usage of s///

by smls (Friar)
on Sep 03, 2013 at 16:16 UTC ( #1052137=note: print w/ replies, xml ) Need Help??


in reply to strange usage of s/// [solved: Perl 5.14 and 5.16 bug]

Try YAPE::Regex::Explain

PS: The s/@{[s".."."gr]}\K$"// solution does not work for me at all. Does it actually work for you?


Comment on Re: strange usage of s///
Download Code
Re^2: strange usage of s///
by davido (Archbishop) on Sep 03, 2013 at 17:26 UTC

    Neigher \K nor /r existed in Perl 5.6.x, which is the latest version of the regexp syntax that YAPE::Regex::Explain supports. (This is documented, and would have been pretty easy to test.)

    perl -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new(q/@{[s". +."."gr]}\K$"/)->explain'

    ...produces the following output (assuming a Perl that is recent enough to understand \K and /r):

    The regular expression: (?-imsx:@{[s".."."gr]}\K$") matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- @{ '@{' ---------------------------------------------------------------------- [s".."."gr] any character of: 's', '"', '.', '.', '"', '.', '"', 'g', 'r' ---------------------------------------------------------------------- } '}' ---------------------------------------------------------------------- \K 'K' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    ...which obviously is totally wrong. It completely misses that @{[.....]} interpolates code into a string (it instead thinks it's looking at a character class), it mistakes \K for literal "K", and mistakes $" for an end of line/string metacharacter, followed by a literal ", when it really should see the Perl special variable. Add to that the fact that it couldn't possibly understand the /r semantics, and the fact that we're using the regexp engine re-entrantly (which I don't think used to be possible), and it's a hopeless case for poor old YAPE::Regex::Explain.

    I too am confused by why he demonstrated /@{[s".."."gr]}\K$"//; I don't see it mentioned among the submissions I viewed, and I don't see any mechanism for it actually doing what it's supposed to. It certainly doesn't match the code in the link provided.


    Dave

      Neigher \K nor /r existed in Perl 5.6.x, which is the latest version of the regexp syntax that YAPE::Regex::Explain supports. (This is documented, and would have been pretty easy to test.)

      Yes, my bad.

      I made up for it by providing a proper, detailed answer below... :)

Re^2: strange usage of s///
by brx (Pilgrim) on Sep 03, 2013 at 20:13 UTC
    PS: The s/@{[s".."."gr]}\K$"// solution does not work for me at all. Does it actually work for you?

    Sure. This solution was accepted on anarchy golf site.

    perl -e '$_="abcdef";s/@{[s".."."gr]}\K$"//;print"$_\n";' abc def
    English is not my mother tongue.
    Les tongues de ma mère sont "made in France".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-08-28 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls