ASP.NET/C#/REGEX: Test if string fits my format

I want to test if a string fits my special format.

[Empty or Number between 1 and 5000]x[Empty or Number between 1 and 5000]

These should all return true:
5000x5000
x250
x
250x
200x123

These should all return false:
abcx123
200
0x100
100x5001
LVL 16
hankknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guru_samiCommented:
Try this:
^(\s*|[1-5]\d{0,3})x(\s*|[1-5]\d{0,3})$

Open in new window

Try this tool to play with it: Expresso
0
gt2847cSr. Security ConsultantCommented:
guru_sami, what you provided doesn't match valid numbers that start with 6 or above, 6x1 would be valid, but your string won't match that.  Additionally, 5999x which exceeds the range would be permitted.

This regex is a bit more complicated, but testing of edge cases shows it should do what you're looking for:

^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-4][0-9][0-9][0-9]|[5][0][0][0]){0,1}x([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-4][0-9][0-9][0-9]|[5][0][0][0]){0,1}$

Open in new window


It tests cases of 1, 2, 3 or 4 digits in length with range testing.


Here's the little C# scratch program you can use to test with:

using System;
using System.Text.RegularExpressions;

namespace EERegexTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var myregex = new Regex("^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-4][0-9][0-9][0-9]|[5][0][0][0]){0,1}x([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-4][0-9][0-9][0-9]|[5][0][0][0]){0,1}$");

            do
            {
                string testme = Console.ReadLine();

                if (testme == null)
                    return;

                Console.WriteLine(myregex.IsMatch(testme) ? "myregex Matches" : "No Match");
            } while (true);

        }
    }
}

Open in new window



Here's another free tool you can play with:

RegEx Builder
0
guru_samiCommented:
Aah...didn't test enough...thanks for pointing out.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

gt2847cSr. Security ConsultantCommented:
No problem...  I have to do range regex often enough and yours was a lot shorter than I normally do.  I had a brief moment when I was thinking, have I been doing this the hard way all long?
0
guru_samiCommented:
Still testing but how does it look now?
^((\s*[1-9]{0,3})|(\s*|5000)|(\s*|[1-4]\d{0,3}))x((\s*[1-9]{0,3})|(\s*|5000)|(\s*|[1-4]\d{0,3}))$

Open in new window


--Valid
5000x5000
x250
x
250x
200x123
4999x
999x

--Invalid
abcx123
200
100x5001
6000x
900
0x100
600x
0
käµfm³d 👽Commented:
Slightly shorter version (give points to gt2847c, though, as upon further inspection it is the same pattern. His generous use of brackets threw me off at first!):

^(5000|[1-4]\d{3}|[1-9]\d\d|[1-9]\d|[1-9])?x(5000|[1-4]\d{3}|[1-9]\d\d|[1-9]\d|[1-9])?$

Open in new window

0
gt2847cSr. Security ConsultantCommented:
guru_sami, your regex still matches 0x, but closer.  kaufmed, thx for shortening it.  I'll stick that in my bag of tricks.  Always useful to learn something better while helping out...

I do a lot of regex with SSIS input validation, so shorter is better =)
0
käµfm³d 👽Commented:
Just for fun:

^(?![6-9]\d{3}|5\d\d[1-9]|0x)\d{0,4}x(?![6-9]\d{3}|5\d\d[1-9]|0$)\d{0,4}$

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.