1
(@"^\w+(?: \w+){0,8}$"

the above regular expression restrict all the special characters except _ . how would i restrict it.

2 Answers 2

2

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.

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

Comments

0

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.

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.