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-@]"
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.