2

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

4 Answers 4

4

Here you have an option for the regex

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

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

3 Comments

+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,}).+$
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
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.
3

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!

Comments

1

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

4 Comments

Yea I like Turtle's answer \s{2,}
It does not work. The above regular expression says: you must have at least 2 white spaces What I want is you must not have 2 white spaces. I used this in the asp regular expression validation.
BTW, You may love Regulator (osherove.com/tools). It allows you to put in a Regex with sample data. Also LinqPad is FaNtAsTic!
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
0

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

Comments

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.