1

I need a regular expression in Java with the following requirements:

  • can only contain letters and digits
  • must at least contain one digit
  • must contain at least one upperCase letter AND one lowerCase letter

I have tried several expressions that don't work - this is the best so far:

(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*
assylias
  • 321,522
  • 82
  • 660
  • 783
me72921
  • 173
  • 2
  • 10

1 Answers1

3

Anchor your regex, and don't use . which allows anything:

^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]*$

^$ matches beginning and end of string, [a-zA-Z0-9]* makes sure the characters are only those in the character class.

Robin
  • 9,415
  • 3
  • 34
  • 45