[英]How to search for occurrences of more than one space between words in a line
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. 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? 我应该使用什么正则表达式?
[ ]{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]
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,}$/
Search for [ ]{2,}
. 搜索[ ]{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}
. \\s{2,}
在于,它还会与Windows文件上的换行符匹配(其中换行符由CRLF
或\\r\\n
表示,而\\r\\n
与\\s{2}
匹配\\s{2}
。
If you also want to find multiple tabs and spaces, use [ \\t]{2,}
. 如果您还想查找多个制表符和空格,请使用[ \\t]{2,}
。
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 这将删除所有数字,逗号和换行符,但选择中间空格,例如
This regex selects all spaces, you can use this and replace it with a single space 此正则表达式选择所有空格,您可以使用此空格并将其替换为单个空格
\s+
example in python python中的示例
result = re.sub('\s+',' ', data))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.