I would like to remove the dots within a word, such that a.b.c.d becomes abcd, But under some conditions:
- There should be at least 2 dots within the word, For example,
a.bremainsa.b, Buta.b.cis a match. - This should match on 1 or 2 letters only. For example,
a.bb.cis a match (becausea,bbandcare 1 or 2 letters each), butaaa.b.ccis not a match (becauseaaaconsists of 3 letters)
Here is what I've tried so far:
import re
texts = [
'a.b.c', # Should be: 'abc'
'ab.c.dd.ee', # Should be: 'abcddee'
'a.b' # Should remain: 'a.b'
]
for text in texts:
text = re.sub(r'((\.)(?P<word>[a-zA-Z]{1,2})){2,}', r'\g<word>', text)
print(text)
This selects "any dot followed by 1 or 2 letters", which repeats 2 or more times. Selection works fine, but replacement with group, causes only on last match and repetition is ignored.
So, it prints:
ac
abee
a.b
Which is not what I want. I would appreciate any help, thanks.