• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expression in java question!

 
yang tao
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"(?=(.*[0-9].*){2}.*)(?=(.*[a-z].*){2}.*)(?=(.*[A-Z].*){2}.*)([0-9a-zA-Z]{6})"
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
yang tao
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for you help
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

For the benefit of future generations of learners, please post what you used in the end.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol, thank you for your solution
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic