Link to home
Start Free TrialLog in
Avatar of heyday2004
heyday2004

asked on

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!
SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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}") );
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tested mine also and it works correctly
but does it also match "12345678"?
ozo,
heyday only requires 6 digits without r and 5 digits with r at the begining
>> 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");
 "\\A[Rr\\d]?\\d{1,5}\\z"
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
zzynx u r right,
just miss typed it
thank u
>> zzynx u r right, just miss typed it
>> thank u
No problem. Nobody's perfect ;°)
I think i need to changed my old thick glasses :-) and yes "\\d{1,6}|[Rr]\\d{1,5}" is better
I thought my expressions were simpler, and do not match "12345678"
>> 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.;°)
Avatar of cjjclifford
cjjclifford

possibly even simpler - "^[\\dRr]\\d{5}$"
nope, meant to write this: "^[\\dRr]\\d{0-5}$"
That matches "R"
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 :-))
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Conclusion: All roads lead to Rome
Well, lots of roads ;°)
doesn't that match "1\n"?
ozo, nope (the $ stops that)
zzynx, yup, the only problem is reading the map :-)
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!
Avatar of heyday2004

ASKER

hey, guys. sorry came back late. thanks a lot so much. you guys are real regular expression experts. thanks again!
Thanks for accepting