Is there a simple way to remove multiple spaces in a string?

后端 未结 29 1467
星月不相逢
星月不相逢 2020-11-22 08:17

Suppose this string:

The   fox jumped   over    the log.

Turning into:



        
29条回答
  •  不思量自难忘°
    2020-11-22 08:56

    To remove white space, considering leading, trailing and extra white space in between words, use:

    (?<=\s) +|^ +(?=\s)| (?= +[\n\0])
    

    The first or deals with leading white space, the second or deals with start of string leading white space, and the last one deals with trailing white space.

    For proof of use, this link will provide you with a test.

    https://regex101.com/r/meBYli/4

    This is to be used with the re.split function.

提交回复
热议问题