I have a requirement to match and replace the continuous spaces that has from 3-20 spaces to just 3 spaces. However, I want to retain the leading spaces as it is. I tried using the negative lookbehind but it is not working as expected.
>>> import re
>>> a=" command1 command2 abc def command3 ghi"
>>> re.sub("(?!^)\s{3,20}", " ", a)
' command1 command2 abc def command3 ghi'
>>> re.sub("(?<!^)\s{3,20}", " ", a)
' command1 command2 abc def command3 ghi'
Just to re-iterate my requirement, I want to do below changes,
a=" command1 command2 abc def command3 ghi"
to
a=" command1 command2 abc def command3 ghi"
my python ver is 2.7. Can anyone please help?