简体   繁体   中英

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.

If I understand correctly, this should work:

>>> r = re.compile(r'( *[\S]*)(?: +)(\n)?')
>>> s = '       if   not regions    and  default_to_all:\n     foo'
>>> r.sub(' ', s)
   if not regions and default_to_all:
 foo

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM