Regex to remove bit signal noise spikes
<p>I am dealing with RF signals that sometimes have noise spikes.<br>
The input is something like this:<br>
<code>00000001111100011110001111100001110000001000001111000000111001111000
</code>  </p>

<p>Before parsing the data in the signal, I need to remove the spike bits, that are 0's and 1's sequence with a lenght lower than (in this example) 3.</p>

<p>So basically I need to match <code>0000000111110001111000111110000111000000(1)000001111000000111(00)1111000
</code><br>
After match, I replace it by the bit before it, so a clean signal look like this:
<code>00000001111100011110001111100001110000000000001111000000111111111000
</code>  </p>

<p>So far I achieved this with two different Regex:</p>

<pre><code>self.re_one_spikes = re.compile("(?:[^1])(?P&lt;spike&gt;1{1,%d})(?=[^1])" % (self._SHORTEST_BIT_LEN - 1))
self.re_zero_spikes = re.compile("(?:[^0])(?P&lt;spike&gt;0{1,%d})(?=[^0])" % (self._SHORTEST_BIT_LEN - 1))
</code></pre>

<p>Then I iterate on the matches and replace.  </p>

<p>How can I do this with a single regex? And can I use regex to replace different sizes matches?<br>
I tried something like this with no success:</p>

<pre><code>re.compile("(?![\1])([01]{1,2})(?![\1])")
</code></pre>

<p>To match both cases <code>[01]</code>in a single regex, it's simply this:  </p>

<p><code>(?&lt;=([01]))(?:(?!\1)[01]){1,2}(?=\1)</code>  </p>

<p>Expanded  </p>

<pre><code> (?&lt;=                 # Lookbehind for 0 or 1
      ( [01] )             # (1), Capture behind 0 or 1
 )
 (?:                  # Match spike, one to %d times in length
      (?! \1 )             # Cannot be the 0 or 1 from lookbehind
      [01] 
 ){1,2}
 (?= \1 )             # Lookahead, can only be 0 or 1 from capture (1)
</code></pre>

<p>Replace with <code>$1</code> times length of the match ( i.e. length of group 0 ).  </p>

<p>Matches  </p>

<pre><code> **  Grp 0 -  ( pos 40 , len 1 ) 
1  
 **  Grp 1 -  ( pos 39 , len 1 ) 
0  

----------------------------------------

 **  Grp 0 -  ( pos 59 , len 2 ) 
00  
 **  Grp 1 -  ( pos 58 , len 1 ) 
1  
</code></pre>

<p>Benchmark  </p>

<pre><code>Regex1:   (?&lt;=([01]))(?:(?!\1)[01]){1,2}(?=\1)
Options:  &lt; none &gt;
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   2
Elapsed Time:    2.06 s,   2058.02 ms,   2058018 Âµs


50,000 iterations * 2 matches/iteration = 100,000 matches 

100,000 matches / 2 sec's  =  50,000 matches per second
</code></pre>

python regex