(@"^\w+(?: \w+){0,8}$"
the above regular expression restrict all the special characters ex开发者_运维问答cept _ . how would i restrict it.
Use
@"^[^\W_]+(?: [^\W_]+){0,8}$"
to allow everything that \w matches except _.
\W means "any character that isn't matched by \w", so by putting it into a negated character class and adding a _ to that class, we're effectively subtracting _ from \w.*
In other words, [^\W_] means "match any character that is neither a non-alphanumeric character nor an underscore".
Another way (perhaps more explicit and easier to understand) would be to use Unicode properties:
@"^[\p{L}\p{N}]+(?: [\p{L}\p{N}]+){0,8}$"
where [\p{L}\p{N}] means "any Unicode letter or number".
*In .NET, the
\w shorthand matches a lot more than [A-Za-z0-9_], especially international (non-ASCII) letters.Replace \w with [a-zA-Z0-9]. The shortcut \w matches any word character, i.e., alphanumeric characters and the underscore.
@"^[a-zA-Z0-9]+(?: [a-zA-Z0-9]+){0,8}$"
You can use [a-z0-9] once you figure out how to set the i flag (case-insensitive) on.
加载中,请稍侯......
精彩评论