Regex to find continuous characters in the word and remove the word
<p>I want to find whether a particular character is occurring continuously in the a word of the string or find if the word contains only numbers and remove those as well. For example,</p>

<pre><code>df
All aaaaaab the best 8965
US issssss is 123 good 
qqqq qwerty 1 poiks
lkjh ggggqwe 1234 aqwe iphone5224s
</code></pre>

<p>I want to check for two conditions, where in the first condition check for repeating characters more than 3 times and also check if a word  contains only numbers. I want to remove only when the word contains only numbers and when a character occurs more than 3 times continuously in the word.</p>

<p>the following should be the output,</p>

<pre><code>df
All the best 
US is good 
qwerty poiks
lkjh aqwe iphone5224s
</code></pre>

<p>The following are my trying,</p>

<p><code>re.sub('r'\w[0-9]\w*', df[i])</code> for number. but this is not removing single character numbers. Also for the repeated characters, I tried, <code>re.sub('r'\w[a-z A-Z]+[a-z A-Z]+[a-z A-Z]+[a-z A-Z]\w*', df[i])</code> but this is removing every word here. instead of repeated letter. </p>

<p>Can anybody help me in solving these problems?</p>

<p>Numbers are easy:</p>

<pre><code>re.sub(r'\d+', '', s)
</code></pre>

<p>If you want to remove words where the same letter appears twice, you can use capturing groups (see <a href="https://docs.python.org/3/library/re.html" rel="nofollow">https://docs.python.org/3/library/re.html</a>):</p>

<pre><code>re.sub(r'\w*(\w)\1\w*', '', s)
</code></pre>

<p>Putting those together:</p>

<pre><code>re.sub(r'\d+|\w*(\w)\1\w*', '', s)
</code></pre>

<p>For example:</p>

<pre><code>&gt;&gt;&gt; re.sub(r'\d+|\w*(\w)\1\w*', '', 'abc abbc 123 a1')
'abc   a'
</code></pre>

<p>You may need to clean up spaces afterwards with something like this:</p>

<pre><code>&gt;&gt;&gt; re.sub(r' +', ' ', 'abc   a')
'abc a'
</code></pre>

python regex python-2.7 python-3.x