[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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!
0
heyday2004
Asked:
heyday2004
  • 7
  • 6
  • 6
  • +3
5 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 6
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now