0

I have a regular expression to check the data entered in a text box. It allows only alpha numeric plus hyphen (-), underscore (_) and white spaces. It works fine for singe values. But if I enter multiple spaces or underscores, it fails. Below is my expression:

Regex.IsMatch(Text, "^[A-Za-z0-9-_ ]*$")

Please suggest if there is any other way to do this or the expression can be changed.

This expression is used in a multi-line text box, so line breaks have to be included as well.

2
  • 1
    Regex.IsMatch(Text, "^[\w-_\\s*]*$") Commented Jul 18, 2014 at 7:08
  • 1
    Can you show more code? The regex should work fine. Commented Jul 18, 2014 at 7:09

4 Answers 4

3

Try this:

Regex.IsMatch(Text, "^[A-Za-z0-9-_\\s]*$")

My understanding is that \s matches whitespaces including CRLF, which would cover OP's use case of a multiline textbox. Therefore, I believe it would be better to replace " " with "\s".

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but any explanation as to why that should work better than the regex OP currently has?
@Jerry I've added my explanation. Do let me know how it can be improved.
This doesn't solve the OP's original problem, which was related to multi-line strings. This will fail for strings containing line-breaks.
2

Replace the space character with \s to allow any whitespace character.

Regex.IsMatch(Text, @"^[a-zA-Z0-9\s_-]*$")

String text = @"foo-
bar  baz _____________     
quz_";

Regex.IsMatch(text, "^[a-zA-Z0-9-_ ]*$");    // False
Regex.IsMatch(text, @"^[a-zA-Z0-9\s_-]*$");  // True

4 Comments

Hmm doesn't explain why multiple underscores fail. And being multi-line is an assumption.
Not with String text = @"____"; (no newline), although OP never mentioned multiline text.
OP edited question and removed the multiline comment. What I meant to say is: it doesn't explain why multiple underscores will fail the regex "^[a-zA-Z0-9-_ ]*$"
@Jerry, It failed on space characters because they most likely were newline characters or such, so if OP had multiple underscores in the multiline text box; of course it will fail.
1

You should actually change the Regular Expression into using defined tags for whitespace characters (\s) and alphanumeric values (\w).

\s will take care of spaces, tabs and line breaks (which you need for the multi line input)
\w will take care of alphanumeric and underscore.
The only other thing you then need is hyphen, which is simply just inputting a hyphen.

This makes your final Regex look like this:

^[\w\s-]*$

And the C# could would be:

Regex.IsMatch(Text, @"^[\w\s-]*$")

A good place to test your regular expressions and get help would be a page like RegExr: http://www.regexr.com/

Comments

1

You need to add an option to allow it to work multi-line. It won't be the multiple spaces or underscores breaking it, but rather line breaks.

Change your regex to the following, and it should work...

Regex.IsMatch(Text, "^[A-Za-z0-9-_ ]*$",RegexOptions.Multiline)

As others suggested, it's also better to use \s instead of a literal space. Add the IgnoreCase option to make regex simpler. Finally escape your hyphen for clarity.

vb.net

Regex.IsMatch(Text, "^[a-z0-9\-_\s]*$",RegexOptions.Multiline or RegexOptions.IgnoreCase)

C#

Regex.IsMatch(Text, @"^[a-z0-9\-_\s]*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.