in reply to Re: Regex: matching character which happens exactly once
in thread Regex: matching character which happens exactly once
Duh, it's actually much simpler, and this one even captures the single character(s)! (Update 2: See Variable-Width Lookbehind (hacked via recursion))
/(.)(?!((?<=(?!\1).(?=(?2)).|(?=\1)..))|.*\1)/s
Of course, right after I said I was "done" with this topic, I realized that the "recursive lookbehind" technique can be used on its own, without all that complicated lookahead logic, since that's what the regex engine and backtracking are for ;-)
qr{ (?<cur> . ) #(?(?= (?&lookback) | .* \g{cur} ) # (?{print "cur($+{cur}) seen before or after\n"}) # (*FAIL) #) # simpler: (?! (?&lookback) | .* \g{cur} ) (?{ print "post: $-[0]<$&>$+[0]\n" }) (?(DEFINE) (?<lookback> (?<= (?<prev> (?! \g{cur} ) . ) (?{ print " prev: <$+{prev}> / $-[0]<".($&//'-').">$+[0]\ +n" }) (?=(?&lookback)) . | (?= \g{cur} ) . . ) ) ) }msx
Update: Another smaller simplification ((*FAIL) isn't needed).
|
|---|