开发者

Regex to Exclude Double spaces

开发者 https://www.devze.com 2022-12-27 21:59 出处:网络
I am looking for a regular expression for c# asp.net 3.5 that will fail if there are ever any double spaces in a sentence or group of words.

I am looking for a regular expression for c# asp.net 3.5 that will fail if there are ever any double spaces in a sentence or group of words.

the cat chased t开发者_运维技巧he dog = true
the  cat  chased  the dog = false (doubles spaces occur at random intervals)

thanks


Try

^((?!\s{2}).)*$

In this expression (?!\s{2}). matches every character except whitespace ones, followed by another whitespace.


Do you even need to use regexs? Why not try:

string test = "the  cat  chased  the dog";
bool containsDoubleSpaces = test.Contains("  ");


your regexp is just this : " +" (that's 2 spaces with a + after them)

it will match 2 or more spaces in a row.


^.* .*$ or even    (just two spaces) would do the trick. Replace spaces with \s if you wish to accomodate any two whitespace charachers in succession (tabs, new lines etc)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号