in reply to Re: Regex: matching character which happens exactly once
in thread Regex: matching character which happens exactly once

This can't work, because the first \1 will be always set to the last match ahead (and undef or "" at first encounter)

This is because $1 is a global var will keep match instead of erasing when backtracking.

FWIW I tried something similar by capturing the following character in $2 for the next run:

m/ ^ (?:(?!\2).)*? (.) (?=(.|$)) (?!.*\1) /x

But couldn't get it to work, probably because the regex engine is not considering another defined \2 while backtracking. (or probably b/c I was too tired last night)

DB<310> @inp = glob '{a,b}'x3 DB<311> ;m/ ^ (?:(?!\2).)*? (?{say "<$_ $2>"}) (.) (?=(.|$)) (?!.* +\1) /x and say ("found $1 in $_") for @inp <aaa > <aab > <aba > <aba b> found b in aba <abb > found a in abb <baa > found b in baa <bab > <bab a> found a in bab <bba > <bbb > DB<312>

probably I'm having a bug in my logic, experts to the rescue! ;-)

Didn't have the time yet for proper debugging.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^3: Regex: matching character which happens exactly once (using global memory)
by QM (Parson) on Oct 23, 2017 at 14:46 UTC
    I didn't expect my forward ref to work, if that's what you thought. The regex engine would have to include a flag to indicate when the first capturing parens were seen, and do the right thing.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      What is the right thing???

      I can only judge the implementation, which considers $1 ff to be global.

      update

      This demonstrates whats happening, $2 is always the next character or empty.

      DB<322> ;m/ ^ .*? (?{say "<$_ $2>"}) (.) (?=(.|$)) (*FAIL)/x <babab > <babab a> <babab b> <babab a> <babab b> <babab >

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        You're asking theoretical questions, and then getting bogged down in current implementation.

        Turn this around. What would you want to happen if you had a forward reference?

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of