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>Rhe em dash in your string is a unicode character, which will be interpreted as multiple characters <a href="http://www.fileformat.info/info/unicode/char/2014/index.htm" rel="nofollow">(3 in your case)</a>. Your version of python is not unicode-aware so you'll either need to match 3 characters to capture <code>.{3}</code> the dash, match the character exactly in your expression, or use a different version of python.</p>

<p>A few notes regarding your expression; You should always prefix your regular expression strings with <code>r'...'</code> so that your <code>\</code> escapes will be interpreted correctly.</p>

<p>A <code>.</code> in a regular expression has a special meaning, it will match any single character. If you need a period/decimal point, you need to escape the dot <code>\.</code>.</p>

<pre><code>pattern = re.compile(r'[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3} .')
</code></pre>

python regex python-2.7