I deal with radio signals that sometimes have noise peaks.
The input looks something like this:
00000001111100011110001111100001110000001000001111000000111001111000
Before parsing the data in the signal, I need to remove the peak bits, which are 0 and 1 sequences with a length less than (in this example) 3.
So basically, I need to match. 0000000111110001111000111110000111000000(1)000001111000000111(00)1111000
After the match, I replace its bit before it, so a clean signal looks like this:
00000001111100011110001111100001110000000000001111000000111111111000
So far I have achieved this with two different Regex:
self.re_one_spikes = re.compile("(?:[^1])(?P<spike>1{1,%d})(?=[^1])" % (self._SHORTEST_BIT_LEN - 1))
self.re_zero_spikes = re.compile("(?:[^0])(?P<spike>0{1,%d})(?=[^0])" % (self._SHORTEST_BIT_LEN - 1))
Then I repeat the matches and replace.
How can I do this with a single regex? Can I use regex to replace matches of different sizes?
I tried something like this without success:
re.compile("(?![\1])([01]{1,2})(?![\1])")