wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java regex to find atleast one occurrence of alphanumeric characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java regex to find atleast one occurrence of alphanumeric characters" Watch "Java regex to find atleast one occurrence of alphanumeric characters" New topic
Author

Java regex to find atleast one occurrence of alphanumeric characters

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hi All,

How to find the occurrence of one from each alphanumeric categories in a String?
Upper Case: [A-Z]
Lower Case: [a-z]
Digits: [0-9]

Example:
Aa1Bb2 = returns true
aa1bb = returns false as there is no upper case character
aaBab = returns false as there is no digits.

I am using following regex pattern --> "^[a-zA-Z0-9]+$"
But it not seem seem to return false when any one of the character is missing from the above catogories.

Thanks
Adi
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

While it's possible to do that with a single regex, I wouldn't. I'd create one regex for each condition and test that input.matches(upperRegex) && input.matches(lowerRegex) && input.matches(digitRegex).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Jeff Verdegan wrote:input.matches(upperRegex) && input.matches(lowerRegex) && input.matches(digitRegex).

or just us a single case-insensitive regex for the alphabetical part...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:
Jeff Verdegan wrote:input.matches(upperRegex) && input.matches(lowerRegex) && input.matches(digitRegex).

or just us a single case-insensitive regex for the alphabetical part...


That won't work if it has to have at least one upper and at least one lower.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aditya Sirohi wrote:
I am using following regex pattern --> "^[a-zA-Z0-9]+$"
But it not seem seem to return false when any one of the character is missing from the above catogories.


Your regex doesn't say anything about the individual catagories. It just says that the string must be at least one character long and every character must be lowercase, uppercase, or digit.
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

I did this, but it does not gives print true. Am i missing something?

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

This is a dirty code, but it solves the purpose. Any idea to optimize it?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aditya Sirohi wrote:I did this, but it does not gives print true. Am i missing something?



Think carefully about what each of those regexes says, and what you actually WANT them to say.
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

I am missing something in the REGEX, what the current REGEX do is to look for occurrence of [A-Za-z0-9] in the input String? I am not sure.. sorry!!!
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Thanks for making me think!!

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aditya Sirohi wrote: Thanks for making me think!!


You're welcome. Glad you got it!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Jeff Verdegan wrote:
fred rosenberger wrote:
Jeff Verdegan wrote:input.matches(upperRegex) && input.matches(lowerRegex) && input.matches(digitRegex).

or just us a single case-insensitive regex for the alphabetical part...


That won't work if it has to have at least one upper and at least one lower.

oh...i mis-read the original post. sorry.
 
wood burning stoves
 
subject: Java regex to find atleast one occurrence of alphanumeric characters