tags:

views:

158

answers:

4

How can I write regular expression in C# to validate that the input does not contain double spaces? I am using Regular Expression Validation. However I do not know what is the Validation Expression to get the result.

"white snake"  : success
"white  snake" : fail
A: 

Search in the input for the substring "  ". If it is found, the input is invalid.

sth
+1  A: 

Use the regular expression {2,} (the first character of that regex is a space).

Turtle
Yea I like Turtle's answer \s{2,}
Dr. Zim
It does not work.The above regular expression says:you must have at least 2 white spacesWhat I want isyou must not have 2 white spaces.I used this in the asp regular expression validation.
BTW, You may love Regulator (http://osherove.com/tools). It allows you to put in a Regex with sample data. Also LinqPad is FaNtAsTic!
Dr. Zim
Just set the boolean to not.
Dr. Zim
Thanks for the reply.If you do it in C#, I agree with you to use NOT.However, in asp.net - you cannot do it.You have to specify correct regular expression in ValidationExpression
+3  A: 

Here you have an option for the regex

^((?!\s\s).)*$

Claudio Redi
+1 but be aware that this allows empty entries. Changing the `*` to `+` will require at least one character to be present: `^((?!\s\s).)+$` or alternately `^(?!.*\s{2,}).+$`
Ahmad Mageed
well...technically he never said that empty is an invalid entry :-) "validate that the input does not contain double spaces" an empty entry doesn't contain 2 white spaces
Claudio Redi
Be aware that this will refuse entries with any two white space characters, not just spaces. For example, it will reject a field with a blank line separating paragraphs.
Turtle
+2  A: 

You can use the following .net regular expression to validate the explicit example you have provided.

\w+\s{1}\w+

This regular expression has three parts, from left to right it reads like this - any word character at least one time followed by only one space character followed by any word character at least one time. So \w+ represents any word character at least one time and \s{1} represents any whitespace character only one time.

In the case where you have someone inputting more than two words you might solve it using the above regular expression, but with a set of grouping parens added to it and a bit of code to build the correct number of occurrences of this pattern.

We can take the above regular expression and extend it to look for any number of occurrences of this pattern or in other words for more than just two words. For instance if you have three words instead of two you will have to implement a set of grouping parens to explicitly state how many times this pattern should show up in the string.

For instance if we have the word “white snake valley” and we want to make sure it only has one space between each word you could rewrite the regular express as follows:

(\w+\s{1}\w+){2}

We added grouping parens and stated that the expression only evaluates to true if this pattern is found exactly two times {2}.

Enjoy!

Doug