2

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

4 Answers 4

4

Try

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Ian: Just out of curiosity, try it with a non-capturing group: ^(?:(?!\s{2}).)*$
@Ian: No clue why this might be, sorry. Goold old "Works for me". =)
4

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

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

Comments

1

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

it will match 2 or more spaces in a row.

Comments

0

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

1 Comment

could you supply a full regex to exclude the double spaces?thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.