Search code examples
pythonsublimetextsublime-text-plugin

How to remove multiple spaces between words in Python, without the leading spaces


I'm writing a simple Sublime Text plugin to trim extra, unnecessary, spaces between words but without touching the leading spaces not to mess up Python formatting.

I have:

[spaces*******are********here]if****not***regions***and**default_to_all:

and want to get:

[spaces***are***still****here]if not regions and default_to_all:

Thinking about

regions = view.find_all('\w\s{2,}\w')
view.erase(edit, region)

but it cuts out the first and the last letter too.


Solution

  • For non-matching leading spaces implies you want to match multiple spaces following a non-space character (and replace it with single space), so you can replace (?<=\S) +(?=\S) with single space "".

    Explanation:

    (?<=\S) +(?=\S)
    (?<=              Positive look-behind, which means preceded by...
        \S                non-space character
          )           end of look-behind group
            +         more than 1 space
             (?=\S)   Positive look-ahead, which means followed by...
                          non-space character
                      end of look-ahead group
    

    That should be straight-forward to understand. You may need to tweak it a bit for trailing space handling though.

    See "regular expressions 101" for more information.

    However, just as a side note regarding your intention: This is not going to be a reliable way to reformat code. Apart from leading spaces, there are still many cases of multiple-spaces that are significant. The most obvious one is spaces within string literal.