What is a good Regular Expression for 6 digit input validation?

The validation requirement is:
1. user can EITHER input 1 to 6 digit number
2. OR any string starting with R(r) followed by 1 to 5 digit numbers. i.e. any of below input is valid:
1, r2, R23333, 234, 555555

I wrote: ValidationExpression="\d{1,6}" for the first case and worked well. But what is the good regular expression to meet all the two requirements? Thanks a lot!
heyday2004Asked:
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.

petmagdyCommented:
try this:

        boolean b = Pattern.matches("\\d{1,6}|[r]\\d{1,6}|[R]\\d{1,6}","R51235");
        System.out.println(b);
0
objectsCommented:
="\d{1,6}|[Rr]d{1,5}"
0
zzynxSoftware engineerCommented:
objects' comment is almost correct. (one extra backslash needed)

These all print "true":

        System.out.println( "1".matches("\\d{1,6}|[Rr]\\d{1,5}") );
        System.out.println( "r2".matches("\\d{1,6}|[Rr]\\d{1,5}") );
        System.out.println( "R23333".matches("\\d{1,6}|[Rr]\\d{1,5}") );
        System.out.println( "234".matches("\\d{1,6}|[Rr]\\d{1,5}") );
        System.out.println( "55555".matches("\\d{1,6}|[Rr]\\d{1,5}") );
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ozoCommented:
"^(\\d|[Rr\\d]\\d{1,5})$"
0
petmagdyCommented:
I tested mine also and it works correctly
0
ozoCommented:
but does it also match "12345678"?
0
petmagdyCommented:
ozo,
heyday only requires 6 digits without r and 5 digits with r at the begining
0
zzynxSoftware engineerCommented:
>> I tested mine also and it works correctly

It shouldn't match "R123456".

I think yours should be
      boolean b = Pattern.matches("\\d{1,6}|[r]\\d{1,5}|[R]\\d{1,5}","R51235");
0
ozoCommented:
 "\\A[Rr\\d]?\\d{1,5}\\z"
0
zzynxSoftware engineerCommented:
ozo, why exactly trying to find more complicated expressions while a simple and clear "\\d{1,6}|[Rr]\\d{1,5}" just works fine?
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
petmagdyCommented:
zzynx u r right,
just miss typed it
thank u
0
zzynxSoftware engineerCommented:
>> zzynx u r right, just miss typed it
>> thank u
No problem. Nobody's perfect ;°)
0
petmagdyCommented:
I think i need to changed my old thick glasses :-) and yes "\\d{1,6}|[Rr]\\d{1,5}" is better
0
ozoCommented:
I thought my expressions were simpler, and do not match "12345678"
0
zzynxSoftware engineerCommented:
>> I thought my expressions were simpler
That's your good right of course.
No offense meant :)

>> and do not match "12345678"
None of the others did match that one, I think.
LOL. Apparantly we can't "read" each others regular expressions.;°)
0
cjjcliffordCommented:
possibly even simpler - "^[\\dRr]\\d{5}$"
0
cjjcliffordCommented:
nope, meant to write this: "^[\\dRr]\\d{0-5}$"
0
ozoCommented:
That matches "R"
0
cjjcliffordCommented:
argh, third time lucky.... "^[\\dRr]\\d{0,5}$"

(so much for simplicity! I must remember to get the first cup of coffee in before posting in future :-))
0
cjjcliffordCommented:
d'oh... "R"....

this should be it:

"^[\\dRr]?\\d{1,5}$"

(again, I reiterate my coffee comment! lets make that a double espresso!)
0
zzynxSoftware engineerCommented:
Conclusion: All roads lead to Rome
Well, lots of roads ;°)
0
ozoCommented:
doesn't that match "1\n"?
0
cjjcliffordCommented:
ozo, nope (the $ stops that)
zzynx, yup, the only problem is reading the map :-)
0
petmagdyCommented:
I think we r all uselessly dwelling arround and the asker seems not interested any more or maybe already found the solution by himself :-)
cheers!
0
heyday2004Author Commented:
hey, guys. sorry came back late. thanks a lot so much. you guys are real regular expression experts. thanks again!
0
zzynxSoftware engineerCommented:
Thanks for accepting
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.