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!


In reply to Re^2: Regex: matching character which happens exactly once (using global memory) by LanX
in thread Regex: matching character which happens exactly once by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.