search and replace a space with a tab at a specific location in a string
<p>I need some help replacing a space with a tab on a string with multiple spaces.
I need to search the string for a time format such as 08:20:10 and replace the space on the end with a tab.
My code is the following:</p>

<pre><code>alert = '9/14/2016 08:20:10 CH1 This is a test.'
str = re.sub (r'(/d{2}:\d{2}:\d{2})(\s)$', '\t', alert)
</code></pre>

<p>I've been unsuccessful in this endeavor. The output should look like:</p>

<pre><code>9/14/2016 08:20:10    CH1 This is a test.
</code></pre>

<p>What am I doing wrong?</p>

<p>Couple things to fix in your expression:</p>

<ul>
<li><code>/d</code> should have been <code>\d</code></li>
<li>no need for the end of the string match <code>$</code></li>
</ul>

<p>Also, I would use a <a href="http://www.regular-expressions.info/lookaround.html" rel="nofollow">positive look behind</a> instead of a capturing group:</p>

<pre><code>(?&lt;=\d{2}:\d{2}:\d{2})\s
</code></pre>

<p>Works for me:</p>

<pre><code>&gt;&gt;&gt; print(re.sub(r'(?&lt;=\d{2}:\d{2}:\d{2})\s', '\t', alert))
9/14/2016 08:20:10  CH1 This is a test.
</code></pre>

python