C# Regex | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C# Regex

How can I match only letters and spaces? I've come up with [^A-Za-z] but I can't figure out spaces

19th May 2020, 12:28 AM
Daniel Cooper
Daniel Cooper - avatar
4 Answers
+ 5
^[a-zA-Z\s]*$ ^ means start of the string \s means spaces *$ means all what is in brackets till the last element of the string, last element also included
19th May 2020, 12:36 AM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 1
Sebastian Pacurar \s matches any white space character including spaces, tabs, line breaks..
19th May 2020, 4:16 AM
Calviղ
Calviղ - avatar
+ 1
Thanks guys, but I literally forgot about String.Split(), lol. I got it all figured out. Didn't even need to worry about spaces.
19th May 2020, 4:31 AM
Daniel Cooper
Daniel Cooper - avatar
0
([A-Za-z ])+ A-Z matches character from A to Z a-z matches character from a-z matches space character [] matches any character in the set + matches one or more of the preceding token https://regexr.com/54si4
19th May 2020, 12:49 AM
Calviղ
Calviղ - avatar