( DISCLAIMER this is a theoretical question about pure regexes, I know how to solve this in Perl, it's a follow up to this thread)
I'm banging my head at this problem, how do I match strings where at least one character happens exactly once, with a pure regex?
(i.e. without additional Perl code, especially embedded one, and without manipulating the input)
Finding all characters which aren't repeated afterwards is easy with a lookahead assertion
DB<200> p bab DB<200> x / (.) (?! .* \1 ) /gx 0 'a' 1 'b'
but combining with a lookbehind fails, b/c variable length is not permitted
DB<211> x / (.) (?<! \1 .* ) /gx Variable length lookbehind not implemented in regex m/ (.) (?<! \1 .* +) / at (eval 261)[C:/Perl_64/lib/perl5db.pl:646] l ine 2.
(actually already using the backreference \1 fails, since the placeholder has variable length)
So it boils down to the question:
All workarounds I found so far only work with ugly cheats, like hardcoding all cases for a fixed length string only.
I think it might be possible with recursive regexes and relative backreferences, but still ...
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
In reply to Regex: matching character which happens exactly once by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |