我正在尝试编写一个正则表达式,它将捕获两个或多个空白空间,不包括领先的空白空间。以下面这个例子为例
One OS to rule them all,
One OS to find them.
One OS to call them all,
And in salvation bind them.
In the bright land of Linux,
Where the hackers play.
我想让它变成
One OS to rule them all,
One OS to find them.
One OS to call them all,
And in salvation bind them.
In the bright land of Linux,
Where the hackers play.
通过使用这个regex ([ ]* ){2,}
,我可以捕获两个或多个空白空间。这方面的问题是,它还捕获了第2-5行中的领先空格。
注意:,我想在https://www.jetbrains.com/help/idea/2016.1/regular-expression-syntax-reference.html中使用这个正则表达式。
发布于 2016-07-05 14:53:48
发布于 2016-07-05 14:52:13
在您的示例中,您可以使用单词边界元字符:
\b\s{2,}
这将匹配在单词末尾(或开头,但一个单词不能以空格开头)后面的任何大于2的空格。
但是,在更一般的情况下,它会失败,因为在特殊字符后面有多个空格,这不会被认为是单词的一部分。
如果您的语言支持无界宽度查找,则可以匹配以下内容:
(?<!^\s*)\s{2,}
发布于 2016-07-05 15:25:08
有了对(*SKIP)(*FAIL)
的支持,您还可以得到以下内容:
^[ ]+(*SKIP)(*FAIL) # match spaces at the beginning of a line
# these shall fail
| # OR
[ ]{2,} # at least two spaces
请参阅regex101.com上的演示 (注意修饰符!)。
https://stackoverflow.com/questions/38206425
复制相似问题