Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression not matching

I'm trying to create a RegExp to match new passwords (which works), but I only need a last step to make it work 100%.

Here's the RegExp and what it is supossed to:

((?=.*(\\d|[\\(\\)\\{\\}\\?!\\$&\\*%\\=\\+_\\-\\.]))(?=.*[a-z])(?=.*[A-Z]).{8,})

The RegExp says that: Digits OR Symbols (){}?!$&%*=+-. must be used -and that's what doesn't work, the OR operator, as I can insert both numbers and symbols-, at least one lowercase, at least one uppercase and a minimum lenght of 8 characters.

I've tried to use the OR operator | in several ways, but I can't make it work.

What am I missing? Thank you very much in advance.

Note: I'm using this regular expression within a liferay configuration file for the password policies.

like image 302
agapitocandemor Avatar asked Mar 11 '26 20:03

agapitocandemor


2 Answers

Ok, I've rewritten your expression slightly, this is what I came up with:

String pattern = "^(?=.*[\\d().{}?!$&*%=+_-])(?=.*[a-z])(?=.*[A-Z]).{8,}$";

This matches any string with

  • at least one number or special character
  • at least one lower-case letter
  • at least one upper-case letter
  • is at least 8 characters long
like image 189
Keppil Avatar answered Mar 13 '26 08:03

Keppil


You want a XOR logical operation, not a OR.

OR is true:
A |  B |  o/p
T |  T |   T
F |  T |   T
T |  F |   T
F |  F |   F

XOR is true:
A |  B | o/p
T |  T |  F
F |  T |  T
T |  F |  T
F |  F |  F

Exclusive Or in Regular Expression

like image 35
Lee Louviere Avatar answered Mar 13 '26 10:03

Lee Louviere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!