Jeff
Jeff

Reputation: 13199

RegEx for pattern in c#

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

Rules are as below:

Examples:

12345 – allowed
Os342 – allowed
2O3d3 – allowed
3sdfds dsfsdf – not allowed
Sdfdf.sdfdf –now allowed

Upvotes: 1

Views: 110

Answers (3)

Darko
Darko

Reputation: 38860

^[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

Upvotes: 3

YOU
YOU

Reputation: 123791

How about

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

Upvotes: 3

Rubens Farias
Rubens Farias

Reputation: 57926

What about:

string input = "12345";
bool match = Regex.IsMatch(input, "^[a-z0-9]{1,5}$", RegexOptions.IgnoreCase);

Upvotes: 3

Related Questions