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>I would suggest</p>

<pre><code>\s*\b(?=[a-zA-Z\d]*([a-zA-Z\d])\1{3}|\d+\b)[a-zA-Z\d]+
</code></pre>

<p>See the <a href="https://regex101.com/r/qA0aS0/1" rel="nofollow">regex demo</a></p>

<p>Only alphanumeric words are matched with this pattern:</p>

<ul>
<li><code>\s*</code> - zero or more whitespaces</li>
<li><code>\b</code> - word boundary</li>
<li><code>(?=[a-zA-Z\d]*([a-zA-Z\d])\1{3}|\d+\b)</code> - there must be at least 4 repeated consecutive letters or digits in the word OR the whole word must consist of only digits</li>
<li><code>[a-zA-Z\d]+</code> - a word with 1+ letters or digits.</li>
</ul>

<p><a href="http://ideone.com/5OiEtS" rel="nofollow">Python demo:</a></p>

<pre><code>import re
p = re.compile(r'\s*\b(?=[a-z\d]*([a-z\d])\1{3}|\d+\b)[a-z\d]+', re.IGNORECASE)
s = "df\nAll aaaaaab the best 8965\nUS issssss is 123 good \nqqqq qwerty 1 poiks\nlkjh ggggqwe 1234 aqwe iphone5224s"
strs = s.split("\n")                   # Split to test lines individually
print([p.sub("", x).strip() for x in strs])
# =&gt; ['df', 'All the best', 'US is good', 'qwerty poiks', 'lkjh aqwe iphone5224s']
</code></pre>

<p>Note that <code>strip()</code> will remove remaining whitespaces at the start of the string.</p>

<p>A similar solution in R with a TRE regex:</p>

<pre><code>x &lt;- c("df", "All aaaaaab the best 8965", "US issssss is 123 good ", "qqqq qwerty 1 poiks", "lkjh ggggqwe 1234 aqwe iphone5224s")
p &lt;- " *\\b(?:[[:alnum:]]*([[:alnum:]])\\1{3}[[:alnum:]]*|[0-9]+)\\b"
gsub(p, "", x)
</code></pre>

<p>See a <a href="http://ideone.com/k51nyu" rel="nofollow">demo</a></p>

<p><em>Pattern details</em> and <a href="https://regex101.com/r/sL0wE8/1" rel="nofollow">demo</a>:</p>

<ul>
<li><code>\s*</code> - 0+ whitespaces</li>
<li><code>\b</code> - a leading word boundary</li>
<li><code>(?:[[:alnum:]]*([[:alnum:]])\1{3}[[:alnum:]]*|[0-9]+)</code> - either of the 2 alternatives:

<ul>
<li><code>[[:alnum:]]*([[:alnum:]])\1{3}[[:alnum:]]*</code> - 0+ alphanumerics followed with the same 4 alphanumeric chars, followed with 0+ alphanumerics</li>
<li><code>|</code>  - or</li>
<li><code>[0-9]+</code> - 1 or more digits</li>
</ul></li>
<li><code>\b</code> - a trailing word boundary</li>
</ul>

<p>UPDATE:</p>

<p>To also add an option to remove 1-letter words you may use</p>

<ol>
<li><strong>R</strong> (add <code>[[:alpha:]]|</code> to the alternation group): <code>\s*\b(?:[[:alpha:]]|[[:alnum:]]*([[:alnum:]])\1{3}[[:alnum:]]*|[0-9]+)\b</code> (see <a href="https://regex101.com/r/sL0wE8/3" rel="nofollow">demo</a>)</li>
<li><strong>Python</strong> lookaround based regex (<a href="https://regex101.com/r/qA0aS0/2" rel="nofollow">add</a> <code>[a-zA-Z]\b|</code> to the lookahead group): <code>*\b(?=[a-zA-Z]\b|\d+\b|[a-zA-Z\d]*([a-zA-Z\d])\1{3})[a-zA-Z\d]+</code></li>
</ol>

python regex python-2.7 python-3.x