Regular Expression Dot not working
<p>So I'm trying to parse through a file and I have the following code:</p>

<pre><code>def learn_re(s):
pattern=re.compile("[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3} .")
if pattern.match(s):
    return True
return False
</code></pre>

<p>This matches with "01:01:01.123 â"; however, when I add in one more character, it fails to work. For example if I edit my code so that it's </p>

<pre><code>def learn_re(s):
pattern=re.compile("[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3} . C")
if pattern.match(s):
    return True
return False
</code></pre>

<p>This fails to match with "01:01:01.123 â C" What's happening here? </p>

<p>The issue is that your â is a unicode character. When in a <code>str</code>, it actually behaves more like several characters:</p>

<pre><code>&gt;&gt;&gt; print len('â')
3
</code></pre>

<p>But, if you use a <code>unicode</code> instead of a <code>str</code>:</p>

<pre><code>&gt;&gt;&gt; print len(u'â')
1
</code></pre>

<p>And so, the following will print <code>True</code>:</p>

<pre><code>def learn_re(s):
    pattern=re.compile("[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3} . C")
    if pattern.match(s):
        return True
    return False

print learn_re(u"01:01:01.123 â C")
</code></pre>

<p>Note that this behavior is specific to python 2. In python 3, <code>str</code> and <code>unicode</code> are merged into a single <code>str</code> type, and so this distinction is not required.</p>

python regex python-2.7