How to search for occurrences of more than one space between words in a line

后端 未结 5 2023
青春惊慌失措
青春惊慌失措 2020-12-07 13:46

How to search for occurrences of more than one space between words in a line

1. this is a line containing  2 spaces
2. this is a line containing   3 spaces
3         


        
5条回答
  •  轮回少年
    2020-12-07 14:20

    Search for [ ]{2,}. This will find two or more adjacent spaces anywhere within the line. It will also match leading and trailing spaces as well as lines that consist entirely of spaces. If you don't want that, check out Alexander's answer.

    Actually, you can leave out the brackets, they are just for clarity (otherwise the space character that is being repeated isn't that well visible :)).

    The problem with \s{2,} is that it will also match newlines on Windows files (where newlines are denoted by CRLF or \r\n which is matched by \s{2}.

    If you also want to find multiple tabs and spaces, use [ \t]{2,}.

提交回复
热议问题