What is the regular exp for a text that can't contain any special characters except space?
[\w\s]*
\w
will match [A-Za-z0-9] and the \s
will match whitespaces.
[\w ]*
should match what you want.
If you just want alphabets and spaces then you can use: @"[A-Za-z\s]+" to match at least one character or space. You could also use @"[A-Za-z ]+" instead without explicitly denoting the space.
Otherwise please clarify.
Assuming "special characters" means anything that's not a letter or digit, and "space" means the space character (ASCII 32):
^[A-Za-z0-9 ]+$
You need @"^[A-Za-z0-9 ]+$"
. The \s
character class matches things other than space (such as tab) and you since you want to match sure that no part of the string has other characters you should anchor it with ^
and $
.
Because Prajeesh only wants to match spaces, \s will not suffice as it matches all whitespace characters including line breaks and tabs.
A character set that should universally work across all RegEx parsers is:
[a-zA-Z0-9\0x0020]
Further control depends on your needs. Word boundaries, multi-line support, etc... I would recommend visiting Regex Library which also has some links to various tutorials on how Regular Expression Parsing works.