• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

regular expression in java question!

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"(?=(.*[0-9].*){2}.*)(?=(.*[a-z].*){2}.*)(?=(.*[A-Z].*){2}.*)([0-9a-zA-Z]{6})"
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for you help
 
Campbell Ritchie
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol, thank you for your solution
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic