I am trying to make the user input exactly 4 characters with no spaces... here's what I have:
.[^\s]{4}
but everything I enter says that it didn't match the regex...
Where am I going wrong?
I am trying to make the user input exactly 4 characters with no spaces... here's what I have:
.[^\s]{4}
but everything I enter says that it didn't match the regex...
Where am I going wrong?
Why is there an extra .
in front? That will match a separate character before your "four non-spaces" get counted.
You probably also want to bind it to the beginning and end of the string, so:
^[^\s]{4}$
Your making it more complicated than it needs to be and using \S
, not \s
so you don't match spaces. I think this should work for you:
^[\S]{4}$
Definition of solution:
^ - begins with
[/S] - capital 'S' means no white space
{4} - match 4 times
$ - end of string
\S{4}
will match 4 non-whitespace characters. If you are using c# regex, I highly recommend Expresso.
Alright, edited out my less-good answer, since a better version was covered above, but figured I'd leave this part of my answer, since some might find it useful:
When testing C# regular expressions, I've found this site to be really helpful, as it allows you to quickly and easily test the expressions against various sample inputs, and with different RegEx flags set.