简体   繁体   English

如何搜索一行中单词之间不止一个空格的出现

[英]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 这将删除所有数字,逗号和换行符,但选择中间空格,例如

  • 20171106,16632 ESCG0000018SB 20171106,16632 ESCG0000018SB
  • 20171107,280 ESCG0000018SB 20171107,280 ESCG0000018SB
  • 20171106,70476 ESCG0000018SB 20171106,70476 ESCG0000018SB

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.

相关问题 正则表达式查询:如何在PDF中搜索一个短语,其中该短语中的单词出现在多行上? - Regex query: how can I search PDFs for a phrase where words in that phrase appear on more than one line? C#中的正则表达式可检测单词之间的多个空格 - Regular expressions in C# to detect more than one space between words 如何查找出现x个(以及一个或多个)字母的所有单词? - How to find all words with x (and one or more) occurrences of a letter? Notepad ++正则表达式删除单词后跟多个空格 - Notepad++ regex to remove words followed by more than one space 两个单词之间用空格隔开,然后用逗号分隔多次 - Two words separated by a space followed by a comma more than one time 正则表达式替换换行符,如果它们出现超过一次,包括中间有空格的组合 - Regex replace line feeds if they occur more than one time including combination with space in between 正则表达式匹配两个单词的第一次出现之间的所有内容,两个单词都出现多次 - Regular expression to match everything between first occurrences of two words, both of which appear more than once 搜索文件中不超过两次出现的字符串 - Search file for no more than two occurrences of string 正则表达式匹配多个空格之间的所有组 - Regex to match all groups between more than one space 如何检查PHP中是否有多个空格或开头有空格 - How to check if more than one space or beginning has space in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM