1

I am still learning RegEx so I need some help on this one.

Rules are as below:

  • Min/Max Length of string: 1-5
  • String has to be a whole word
  • String can contain: A-Z and/or a-z and/or 0-9 or combination of both only (e.g. _ - . * are not allowed)

Examples:

12345 – allowed
Os342 – allowed
2O3d3 – allowed
3sdfds dsfsdf – not allowed
Sdfdf.sdfdf –now allowed
9
  • Regex in which programming language/utility, please? Commented Jan 13, 2010 at 1:15
  • I am doing it in C#. But why doesn't matter? Commented Jan 13, 2010 at 1:16
  • 1
    C# RegEx tester: derekslager.com/blog/posts/2007/09/… Commented Jan 13, 2010 at 1:16
  • Because every language has slightly different regexp syntax. Which is one reason I probably can't help you much, I don't know C#. Commented Jan 13, 2010 at 1:17
  • 1
    There are slightly different regex implementations in different languages. Even Visual Studio itself is using non-.NET regex implementation in it's find/replace dialog: codinghorror.com/blog/archives/000633.html Commented Jan 13, 2010 at 1:19

3 Answers 3

3

What about:

string input = "12345";
bool match = Regex.IsMatch(input, "^[a-z0-9]{1,5}$", RegexOptions.IgnoreCase);
Sign up to request clarification or add additional context in comments.

3 Comments

Without ^ and $, the regexp tester he gave me matched "12345" of "123456"; apparently that should be rejected though.
And you were right about those other chars. I deleted my answer, thanks.
@Jeffrey: I used RegexOptions.IgnoreCase, so will not differ a-z from A-Z (as OP said is working with C#). But you can remove it and to add A-Z if you wish
3

How about

^[A-Za-z0-9]{1,5}$

Comments

3
^[a-zA-Z0-9]{1,5}$
  • [a-zA-Z0-9] specifies the ranges allowed
  • ^ specifies start of string
  • $ specifies end of string
  • {1,5} indicates minimum and maximum number of characters for the range

1 Comment

You want to do 1,5 to catch strings shorter than 5 chars too, I think.

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.