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

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

Suppose this string:

The   fox jumped   over    the log.

Turning into:



        
29条回答
  •  再見小時候
    2020-11-22 08:57

    In some cases it's desirable to replace consecutive occurrences of every whitespace character with a single instance of that character. You'd use a regular expression with backreferences to do that.

    (\s)\1{1,} matches any whitespace character, followed by one or more occurrences of that character. Now, all you need to do is specify the first group (\1) as the replacement for the match.

    Wrapping this in a function:

    import re
    
    def normalize_whitespace(string):
        return re.sub(r'(\s)\1{1,}', r'\1', string)
    
    >>> normalize_whitespace('The   fox jumped   over    the log.')
    'The fox jumped over the log.'
    >>> normalize_whitespace('First    line\t\t\t \n\n\nSecond    line')
    'First line\t \nSecond line'
    

提交回复
热议问题