Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to check if a password contain at least one lower case letter, one upper case letter, one digit and one special character.

i'm trying this:

if(!password.matches("(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])")){
        username = "Error";
    }

but give me an error saying: invalid escape sequence.

Someone can help me to solve the problem and can confirm that is a correct pattern?

Thanks, whit \d don't do error but it don't match with a string like Paul%88 why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
377 views
Welcome To Ask or Share your Answers For Others

1 Answer

Java will treat inside a string as starting an escape sequence. Make sure you use \ instead (so that you get an actual character in the string) and you should be ok.

Quick Update: As Etienne points out, if you actually want a in the RegEx itself, you'll need to use \\, since that will produce \ in the string, which will produce in the RegEx.

New Question Update: You mention that your RegEx doesn't work, and I'm pretty sure that's because it's wrong. If you just want to ensure one of each type of character class is present, you may just want to create one RegEx for each class, and then check the password against each one. Passwords are pretty much guaranteed to be short (and you can actually control that yourself) so the perf hit should be minimal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...