简体   繁体   English

正则表达式匹配超过 2 个空格但不匹配新行

[英]Regex to match more than 2 white spaces but not new line

I want to replace all more than 2 white spaces in a string but not new lines, I have this regex: \\s{2,} but it is also matching new lines.我想替换字符串中所有超过 2 个空格而不是新行,我有这个正则表达式: \\s{2,}但它也匹配新行。

How can I match 2 or more white spaces only and not new lines?如何仅匹配 2 个或更多空格而不匹配新行?

I'm using c#我正在使用 c#

Put the white space chars you want to match inside a character class.将要匹配的空白字符放入字符类中。 For example:例如:

[ \t]{2,}

matches 2 or more spaces or tabs.匹配 2 个或更多空格或制表符。

You could also do:你也可以这样做:

[^\S\r\n]{2,}

which matches any white-space char except \\r and \\n at least twice (note that the capital S in \\S is short for [^\\s] ).其中任何空白字符匹配除了\\r\\n的至少两倍(注意,资本S\\S是短期的[^\\s]

Regex to target only two spaces: [ ]{2,} Brackets in regex is character class.正则表达式只针对两个空格: [ ]{2,} 正则表达式中的括号是字符类。 Meaning just the chars in there.意思只是那里的字符。 Here just space.这里只是空间。 The following curly bracket means two or more times.下面的大括号表示两次或多次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM