Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java regex to find atleast one occurrence of alphanumeric characters

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did this, but it does not gives print true. Am i missing something?

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a dirty code, but it solves the purpose. Any idea to optimize it?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for making me think!!

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Sirohi wrote: Thanks for making me think!!


You're welcome. Glad you got it!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic