正则表达式匹配2个以上空格,但不是新的生产线 [英] Regex to match more than 2 white spaces but not new line

查看:367
本文介绍了正则表达式匹配2个以上空格,但不是新的生产线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要替换字符串中的所有超过2空格,但不是新的行,我有这样的正则表达式: \ S {2} 但它也可选配新的线路。

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个或多个空格,而不是只新线?

How can I match 2 or more white spaces only and not new lines?

我使用C#

推荐答案

将要匹配字符类中的空白字符。例如:

Put the white space chars you want to match inside a character class. For example:

[ \t]{2,}

匹配2个或多个空格或制表符。

matches 2 or more spaces or tabs.

您也可以这样做:

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

这除了 \ r匹配任何空白字符至少两次(注意资金取值 \ S 是短期的 [^ \ s]

which matches any white-space char except \r and \n at least twice (note that the capital S in \S is short for [^\s]).

这篇关于正则表达式匹配2个以上空格,但不是新的生产线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆