Regular Expression Matching First Non-Repeated Character
<p><strong>TL;DR</strong></p>

<p><code>re.search("(.)(?!.*\1)", text).group()</code> doesn't match the first non-repeating character contained in text (it always returns a character at or before the first non-repeated character, or before the end of the string if there are no non-repeated characters. My understanding is that re.search() should return None if there were no matches).
I'm only interested in understanding why this regex is not working as intended using the Python <code>re</code> module, not in any other method of solving the problem</p>

<p><strong>Full Background</strong></p>

<p>The problem description comes from <a href="https://www.codeeval.com/open_challenges/12/">https://www.codeeval.com/open_challenges/12/</a>. I've already solved this problem using a non-regex method, but revisited it to expand my understanding of Python's <code>re</code> module.
The regular expressions i thought would work (named vs unnamed backreferences) are: </p>

<p><code>(?P&lt;letter&gt;.)(?!.*(?P=letter))</code> and <code>(.)(?!.*\1)</code> (same results in python2 and python3)</p>

<p>My entire program looks like this</p>

<pre><code>import re
import sys
with open(sys.argv[1], 'r') as test_cases:
    for test in test_cases:
        print(re.search("(?P&lt;letter&gt;.)(?!.*(?P=letter))",
                        test.strip()
                       ).group()
             )
</code></pre>

<p>and some input/output pairs are:</p>

<pre><code>rain | r
teetthing | e
cardiff | c
kangaroo | k
god | g
newtown | e
taxation | x
refurbished | f
substantially | u
</code></pre>

<p>According to what I've read at <a href="https://docs.python.org/2/library/re.html">https://docs.python.org/2/library/re.html</a>:</p>

<ul>
<li><code>(.)</code> creates a named group that matches any character and allows later backreferences to it as <code>\1</code>. </li>
<li><code>(?!...)</code> is a negative lookahead which restricts matches to cases where <code>...</code> does not match.</li>
<li><code>.*\1</code> means any number (including zero) of characters followed by whatever was matched by <code>(.)</code> earlier</li>
<li><code>re.search(pattern, string)</code> returns only the first location where the regex pattern produces a match (and would return None if no match could be found)</li>
<li><code>.group()</code> is equivalent to <code>.group(0)</code> which returns the entire match</li>
</ul>

<p>I think these pieces together should solve the stated problem, and it does work like I think it should for most inputs, but failed on <code>teething</code>. Throwing similar problems at it reveals that it seems to ignore repeated characters if they are consecutive:</p>

<pre><code>tooth | o      # fails on consecutive repeated characters
aardvark | d   # but does ok if it sees them later
aah | a        # verified last one didn't work just because it was at start
heh | e        # but it works for this one
hehe | h       # What? It thinks h matches (lookahead maybe doesn't find "heh"?)
heho | e       # but it definitely finds "heh" and stops "h" from matching here
hahah | a      # so now it won't match h but will match a
hahxyz | a     # but it realizes there are 2 h characters here...
hahxyza | h    # ... Ok time for StackOverflow
</code></pre>

<p>I know lookbehind and negative lookbehind are limited to 3-character-max fixed length strings, and cannot contain backreferences even if they evaluate to a fixed length string, but I didn't see the documentation specify any restrictions on negative lookahead.</p>

<p>Well let's take your <code>tooth</code> example - here is what the regex-engine does (a lot simplified for better understanding)</p>

<p>Start with <code>t</code> then look ahead in the string - and fail the lookahead, as there is another <code>t</code>.</p>

<pre><code>tooth
^  Â°
</code></pre>

<p>Next take <code>o</code>, look ahead in the string - and fail, as there is another <code>o</code>.</p>

<pre><code>tooth
 ^Â°
</code></pre>

<p>Next take the second <code>o</code>, look ahead in the string - no other <code>o</code> present - match it, return it, work done.</p>

<pre><code>tooth
  ^
</code></pre>

<p>So your regex doesn't match the first unrepeated character, but the first one, that has no further repetitions towards the end of the string.</p>

python regex regex-lookarounds