File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes A regex problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "A regex problem" Watch "A regex problem" New topic
Author

A regex problem

Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
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.


SCJP 5.0, SCWCD in progress
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
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

Joined: Jul 11, 2001
Posts: 14112
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 ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Just iterate over the String, and keep count of each of the character classes. Then afterwards check if your requirements are met.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A regex problem