Reputation: 39
I would like to know how to write a regex in c# that allows only
numbers, letters, spaces and the @
symbol.
Valid inputs are:
Abc
Abc def
@Abc
Abc@
Example I tried so far: @"[^\w-\s-@]"
Upvotes: 1
Views: 6096
Reputation: 626748
You may use
@"^[A-Za-z0-9\s@]*$"
See the regex demo
The pattern matches:
^
- start of the string[A-Za-z0-9\s@]*
- zero or more (*
, if you do not want to match an empty string, use +
, 1 or more) occurrences of ASCII letters, digits, any whitespace and @
chars$
- end of string (replace with \z
if you do not want to match if the matching string ends with \n
char).Upvotes: 2