Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A regex problem

 
Mohit Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers

I am trying to create a regular expression for validating a password string for the following given rule -

It must contain atleast any two of following 4 classes of characters -
1. lowercase letters.. a - z
2. uppercase letters.. A - Z
3. digits.. 0 - 9
4. Special characters.. !@#$%^&*()/?><

I m having a hard time creating such regular expression. I am not sure if this forum is the right place to post such problem, but, I will really appreciate if someone can help me with this.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use a regex for this. The regex would be insanely complicated--even if you understand it, nobody else will. Do it the old-fashioned way, with simple program logic, using methods like Character.isDigit() and Character.isUpperCase().
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand the question correctly, this isn't even *possible* using a regular expression. (With other words, the set of valid passwords doesn't constitute a regular language.)
[ September 19, 2008: Message edited by: Ilja Preuss ]
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just iterate over the String, and keep count of each of the character classes. Then afterwards check if your requirements are met.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
If I understand the question correctly, this isn't even *possible* using a regular expression. ...


It is possible. This regex will do the trick:



which really means:



but as Alan pointed out: throwing this voodoo into your code base will result into war with your co-workers!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Piet Verdriet:


It is possible. This regex will do the trick:



which really means:



but as Alan pointed out: throwing this voodoo into your code base will result into war with your co-workers!

Unless I misunderstood the original question, that isn't the requirement. The requirement states that there must be at least two of the classes present, not that there must be two characters from each class present.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:

Unless I misunderstood the original question, that isn't the requirement. The requirement states that there must be at least two of the classes present, not that there must be two characters from each class present.


No, you're right. I thought the OP meant by "two" that all classes should appear twice.


But still, it is possible to do this regex, but it will be even harder to read.




@OP: To make it a bit less complicated, this is what the regex does:



So, you see: as others have pointed out, it will result in an unmaintainable regex-monster. Better do as Alan and Rob suggested!
[ September 20, 2008: Message edited by: Piet Verdriet ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic