C # regex - accept spaces in a string

I have an application that needs some checks for some fields. One of them is a surname, which can consist of two words. In my regex, I have to accept these spaces, so I tried many things, but I did not find a solution.

Here is my regex:

@"^[a-zA-Zàéèêçñ\s][a-zA-Zàéèêçñ-\s]+$"

\s usually for spaces, but it doesn't work, and I got this error message:

parsing "^[a-zA-Zàéèêçñ\s][a-zA-Zàéèêçñ-\s]+$" - Cannot include class \s in character range.

ANY ideas guys?

+5
source share
3 answers

- , , A-Z A Z. ñ-\s, - \s - , \s , \s .

, .

, - , - literal:

@"^[a-zA-Zàéèêçñ\s][a-zA-Zàéèêçñ\s-]+$"

, , \s- , \s - .

- -:

@"^[a-zA-Zàéèêçñ\s][a-zA-Zàéèêç\-\s]+$"

, ñ\-\s , ñ, - \s. , , IMHO .

+9

- - ñ-\s a-z:

@"^[a-zA-Zàéèêçñ\s][a-zA-Zàéèêçñ\-\s]+$"

. Regex Storm: [a-\s], [a\-\s]

+4

[RegularExpression (@ "^ [a-zA-Z \ s] + $", ErrorMessage = "Only alphabetic characters and spaces are allowed.")]

It works

0
source

All Articles