views:

54

answers:

4

Hi,

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 the dog = true
the  cat  chased  the dog = false (doubles spaces occur at random intervals)

thanks

A: 

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

it will match 2 or more spaces in a row.

oedo
A: 

Try

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

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

Jens
Hi This give me a Out of stack space error.
SetiSeeker
@Ian: Just out of curiosity, try it with a non-capturing group: `^(?:(?!\s{2}).)*$`
Alan Moore
@Ian: No clue why this might be, sorry. Goold old "Works for me". =)
Jens
A: 

^.* .*$ 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)

Amarghosh
could you supply a full regex to exclude the double spaces?thanks
SetiSeeker
+3  A: 

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

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