Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: understanding (*SKIP:...)

by ikegami (Patriarch)
on Jun 11, 2025 at 22:05 UTC ( [id://11165375]=note: print w/replies, xml ) Need Help??


in reply to understanding (*SKIP:...)

That (*SKIP:name) precedes (*MARK:name) in your pattern makes no sense, and leads me to think you believe a MARK is akin to a label and a SKIP is akin to a goto. That is not the case at all.

What we're marking and skipping to are positions in the string being matched.

(*MARK:name) bookmarks the current match position under the provided name.
(*SKIP:name) prevents everything before the named bookmarked position from being part of the final match if we backtracking through this.
(*SKIP) prevents everything before the position from being of the final match if we backtracking through this.
(*SKIP) is basically the same as (*MARK:anon)(*SKIP:anon).

Solution:

if ( "aab" =~ /(.)\1(*SKIP)(*FAIL)|.*/ ) { say $&; # b }

(*SKIP) was matched at position 2. When (*FAIL) caused the matching to backtrack through (*SKIP), everything before position 2 was eliminated from potential matches.

Non-trivial example using (*MARK:...):

If instead you want ab, you could use

if ( "aab" =~ /(.)(*MARK:go)\1(*SKIP:go)(*FAIL)|.*/ ) { say $&; # ab }

This time, only the text before position 1 was eliminated from potential matches.

Replies are listed 'Best First'.
Re^2: understanding (*SKIP:...)
by ysth (Canon) on Jun 12, 2025 at 02:06 UTC
Re^2: understanding (*SKIP:...)
by ysth (Canon) on Jun 11, 2025 at 23:09 UTC

    I think I understand everything you are saying, and what I think I wasn't getting is that backtracking through a SKIP immediately terminates the attempt of the regex match at its current position; I wasn't thinking it was a goto, just that somehow I had to do something else to keep the current match attempt from proceeding on other branches.

    I don't think I understand yet why this didn't work, though:

    /(*SKIP:go)(.)(?(?=\1)\1(*MARK:go)(*FAIL)|)/
    Is it simply that the go mark is looked for when the SKIP is encountered, not when it is backtracked through?

      Either that, or the mark is scoped so that backtracking through it forgets it. Or both.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2025-07-08 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.