jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes regular expression in java question! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regular expression in java question!" Watch "regular expression in java question!" New topic
Author

regular expression in java question!

yang tao
Greenhorn

Joined: Sep 25, 2008
Posts: 16
hi guys

i want a string with 2 lower case, 2 upper case, 2 numeric chars in any order such as, 12aaCC, a2A9cC

how can i do that in regular expression?

i am stucked here for a while!

help please

thank you
[ October 06, 2008: Message edited by: yang tao ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
Split the String into a char[] array, sort the array, create a new String, then try a regular expression.

That is one possibility; there are bound to be others.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

"(?=(.*[0-9].*){2}.*)(?=(.*[a-z].*){2}.*)(?=(.*[A-Z].*){2}.*)([0-9a-zA-Z]{6})"


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Sabre:
"(?=(.*[0-9].*){2}.*)(?=(.*[a-z].*){2}.*)(?=(.*[A-Z].*){2}.*)([0-9a-zA-Z]{6})"


That doesn't match the second example, as far as I can tell?


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
yang tao
Greenhorn

Joined: Sep 25, 2008
Posts: 16
thank you for you help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
You're welcome

For the benefit of future generations of learners, please post what you used in the end.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Originally posted by Ilja Preuss:


That doesn't match the second example, as far as I can tell?


It does for me! Test code
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Sabre:


It does for me! Test code


Oh, I see. Stupid me...

Clever regular expression. I wonder whether it might be a bit *too* clever, if you know what I mean...
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Originally posted by Ilja Preuss:


Oh, I see. Stupid me...

Clever regular expression. I wonder whether it might be a bit *too* clever, if you know what I mean...


Esoteric - yes. Clever - not really - just a simple use of 'positive lookahead'.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by James Sabre:
"(?=(.*[0-9].*){2}.*)(?=(.*[a-z].*){2}.*)(?=(.*[A-Z].*){2}.*)([0-9a-zA-Z]{6})"


You could write it a bit more compact:

since you don't need all the dot-stars, which makes it even slightly more readable!
; )
yang tao
Greenhorn

Joined: Sep 25, 2008
Posts: 16
lol, thank you for your solution
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Originally posted by Piet Verdriet:


You could write it a bit more compact:

since you don't need all the dot-stars, which makes it even slightly more readable!
; )


Yes. Much better. I had not taken the logic far enough having failed to deduce that since each of the 'look-a-heads' dictate two or more characters from their acceptance class then one just has to make sure that there are 6 characters total. Nice.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Sabre:

I had not taken the logic far enough having failed to deduce that since each of the 'look-a-heads' dictate two or more characters from their acceptance class then one just has to make sure that there are 6 characters total.


Perhaps you know understand what I mean by "clever"...
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Originally posted by Ilja Preuss:


Perhaps you know understand what I mean by "clever"...


Though the regex I posted worked it was not 'clever'. The improved one is clever.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regular expression in java question!