ediblecode
ediblecode

Reputation: 11971

My RegEx won't allow spaces

I have a regular expression:

^[A-Za-z0-9()]+$

this doesn't allow spaces, commas, fullstops, forward slashes or backslashes

I am a massive amateur when it comes to this. Can anyone just tell me how to get it to allow spaces?

Upvotes: 1

Views: 226

Answers (2)

manojlds
manojlds

Reputation: 301507

Just add a space?

^[A-Za-z0-9() ]+$

Update for comments:

Sample code:

        Regex re = new Regex(@"^[A-Za-z0-9() \\/.-]+$");
        var result = re.Match(@"a. z-1\2/");
        Console.WriteLine(result.Success);

and gives true

Upvotes: 6

Clive
Clive

Reputation: 36955

You can match whitespace with \s:

^[A-Za-z0-9()\s]+$

Upvotes: 5

Related Questions