.NET RegEx for letters and spaces

If you just need English, try this regex:

"^[0-9A-Za-z ]+$"

The brackets specify a set of characters

0-9: All digits

A-Z: All capital letters

a-z: All lowercase letters

' ': Spaces

If you need unicode / internationalization, you can try this regex:

"^[\\w ]+$"

This regex will match all unicode letters and numbers and space, which may be more than you need, so if you just need English or basic Roman characters, the first regex will be simpler and faster to execute.

Note that for both regex I have included the ^ and $ operator which mean match at start and end. If you need to pull this out of a string and it doesn't need to be the entire string, you can remove those two operators.


try this for all letter with space :

@"[\p{L} ]+$"

The character class \w does not match spaces. Try replacing it with [\w ] (there's a space after the \w to match word characters and spaces. You could also replace the space with \s if you want to match any whitespace.

Tags:

C#

.Net

Regex