简体   繁体   中英

java regular expression digits, letters, uppercase, lowercase

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]).*

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM