How to search for occurrences of more than one space between words in a line
如何在一行中的单词之间搜索多个空格的出现次数
1. this is a line containing 2 spaces2. this is a line containing 3 spaces3. this is a line containing multiple spaces first second three four
All the above are valid matches for this regex. What regex should I use?
以上所有都是此正则表达式的有效匹配。我应该使用什么样的正则表达式?
4 个解决方案
#1
126
[ ]{2,}
SPACE (2 or more)
空间(2个或更多)
You could also check that before and after those spaces words follow. (not other whitespace like tabs or new lines)
你也可以检查那些空格之前和之后的单词。 (不是像制表符或新行那样的其他空格)
\w[ ]{2,}\w
the same, but you can also pick (capture) only the spaces for tasks like replacement
同样,但您也可以选择(捕获)仅替换等任务的空间
\w([ ]{2,})\w
or see that before and after spaces there is anything, not only word characters (except whitespace)
或者看到空格之前和之后有什么东西,不仅仅是单词字符(除了空格)
[^\s]([ ]{2,})[^\s]
#2
6
Simple solution:
/\s{2,}/
This matches all occurrences of one or more whitespace characters.If you need to match the entire line, but only if it contains two or more consecutive whitespace characters:
这匹配所有出现的一个或多个空白字符。如果需要匹配整行,但仅当它包含两个或多个连续的空白字符时:
/^.*\s{2,}.*$/
If the whitespaces don't need to be consecutive:
如果空格不需要是连续的:
/^(.*\s.*){2,}$/
#3
3
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.
搜索[] {2,}。这将在线内的任何位置找到两个或更多相邻空格。它还将匹配前导和尾随空格以及完全由空格组成的行。如果您不想这样,请查看Alexander的答案。
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}
.
\ s {2,}的问题在于它还将匹配Windows文件上的换行符(其中换行符由CRLF表示,或者\ r \ n由\ s {2}匹配。
If you also want to find multiple tabs and spaces, use [ \t]{2,}
.
如果您还想查找多个制表符和空格,请使用[\ t] {2,}。
#4
0
Here is my solution
这是我的解决方案
[^0-9A-Z,\n]
This will remove all the digits, commas and new lines but select the middle space such as data set of
这将删除所有数字,逗号和新行,但选择中间空格,如数据集
- 20171106,16632 ESCG0000018SB
- 20171107,280 ESCG0000018SB
- 20171106,70476 ESCG0000018SB
#1
126
[ ]{2,}
SPACE (2 or more)
空间(2个或更多)
You could also check that before and after those spaces words follow. (not other whitespace like tabs or new lines)
你也可以检查那些空格之前和之后的单词。 (不是像制表符或新行那样的其他空格)
\w[ ]{2,}\w
the same, but you can also pick (capture) only the spaces for tasks like replacement
同样,但您也可以选择(捕获)仅替换等任务的空间
\w([ ]{2,})\w
or see that before and after spaces there is anything, not only word characters (except whitespace)
或者看到空格之前和之后有什么东西,不仅仅是单词字符(除了空格)
[^\s]([ ]{2,})[^\s]
#2
6
Simple solution:
/\s{2,}/
This matches all occurrences of one or more whitespace characters.If you need to match the entire line, but only if it contains two or more consecutive whitespace characters:
这匹配所有出现的一个或多个空白字符。如果需要匹配整行,但仅当它包含两个或多个连续的空白字符时:
/^.*\s{2,}.*$/
If the whitespaces don't need to be consecutive:
如果空格不需要是连续的:
/^(.*\s.*){2,}$/
#3
3
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.
搜索[] {2,}。这将在线内的任何位置找到两个或更多相邻空格。它还将匹配前导和尾随空格以及完全由空格组成的行。如果您不想这样,请查看Alexander的答案。
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}
.
\ s {2,}的问题在于它还将匹配Windows文件上的换行符(其中换行符由CRLF表示,或者\ r \ n由\ s {2}匹配。
If you also want to find multiple tabs and spaces, use [ \t]{2,}
.
如果您还想查找多个制表符和空格,请使用[\ t] {2,}。
#4
0
Here is my solution
这是我的解决方案
[^0-9A-Z,\n]
This will remove all the digits, commas and new lines but select the middle space such as data set of
这将删除所有数字,逗号和新行,但选择中间空格,如数据集
- 20171106,16632 ESCG0000018SB
- 20171107,280 ESCG0000018SB
- 20171106,70476 ESCG0000018SB