Yoan
Yoan

Reputation: 39

Regex with Letters, Numbers, @ and spaces

I would like to know how to write a regex in c# that allows only numbers, letters, spaces and the @ symbol.

Valid inputs are:

Example I tried so far: @"[^\w-\s-@]"

Upvotes: 1

Views: 6096

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions