Reputation: 16923
What is the regular exp for a text that can't contain any special characters except space?
Upvotes: 2
Views: 15575
Reputation: 74272
[\w\s]*
\w
will match [A-Za-z0-9_]
and the \s
will match whitespaces.
[\w ]*
should match what you want.
Upvotes: 5
Reputation: 44916
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 ]
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.
Upvotes: 9
Reputation: 64939
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 $
.
Upvotes: 2
Reputation: 75252
Assuming "special characters" means anything that's not a letter or digit, and "space" means the space character (ASCII 32):
^[A-Za-z0-9 ]+$
Upvotes: 4
Reputation: 96557
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.
Upvotes: 1