+ 1

Password validator code coach java

Idk whats wrong https://code.sololearn.com/cN8Z4Vit5Fui/?ref=app

27th Jan 2023, 4:58 PM
Márton Boros
Márton Boros - avatar
11 Answers
+ 2
Your regex not satisfies constraints. Using ^ symbol means exempt. Not includes. So [^0-9]{2,}means anything but not digits from 0 to 9 , at least 2 times. You should use [0-9]{2,} as a one class. You should not include spaces between. [^@, #, ₹,... ] means should not start with @ and dont add commas between. Again [@#*&!$%]{2,} you can use. Next this way, it forcing pattern to have order as digit followed by special chars so it fails when special chars followed by digits...
27th Jan 2023, 5:15 PM
Jayakrishna 🇮🇳
+ 2
As I already said, it fails for @#12345 but it passes 12345@# don't include camma.
27th Jan 2023, 6:12 PM
Jayakrishna 🇮🇳
+ 2
Yes. But still it fail for the above type samples I mentioned. Using Regex is very complex thing for everyone. Even am not sure the correct exact pattern for this task. I need to form and test my pattern, if I want to give you. So it is tricky to explain more for you, if you don't know what are classes in regex, and wild-cards.. etc. Your pattern matches for digit followed by characters but fails if input contains characters before digits. So you need to use a classes for separate constraints. So it ensure order not important. Otherwise, use |(or) operator like ab|ba means ab or ba. Pattern [0-9][ab] means should not end with digit in a way. So you need some more learning about regex. For this task: include case for character followed by digits by using swapped blocks and apply or operator to combine two patterns.. ([0-9][a-z] ) | ([a-z][0-9] ) lile this example.. There exist better simplified solution also. So its a sample only... Hope it helps to complete it.
27th Jan 2023, 6:40 PM
Jayakrishna 🇮🇳
+ 1
Thank you! I forgot to upload that version. But still it fails 2 test cases from 13. I think the problem might be when in the input there are doubles like @@ or 11
27th Jan 2023, 6:02 PM
Márton Boros
Márton Boros - avatar
+ 1
Pattern pattern = Pattern.compile("[0-9]{2,}[#!@$%&*]{2,}", Pattern.CASE_INSENSITIVE ); Like this? Im sorry for wasting your time and thank you for your help!
27th Jan 2023, 6:17 PM
Márton Boros
Márton Boros - avatar
+ 1
Well i tried, but still says “weak” for “@1@1@1@1@1” I guess regex is not the good solution. Just wanted to try:) thanks again!
27th Jan 2023, 10:33 PM
Márton Boros
Márton Boros - avatar
+ 1
Pattern.compile(".*[0-9]{2,}.*[#!@$%&*]{2,}|.*[#!@$%&*]{2,}.*[0-9]{2,}"); I tried this and without .* Still not good. Ive read the official oracle/java site but didnt find a good solution:/ i guess the solution for this codecoach cannot be written this simple:)
28th Jan 2023, 12:22 PM
Márton Boros
Márton Boros - avatar
+ 1
Pattern.compile("(.*[0-9]){2,}(.*[#!@$%&*]){2,}|(.*[#!@$%&*]){2,}(.*[0-9]){2,}"); Try this, you need braces around. .*\\d{2} is search for consecutive digits but (.*\\d){2,} will search for 2 digits in any place...
28th Jan 2023, 12:36 PM
Jayakrishna 🇮🇳
+ 1
Unfortunately i can only give one upvote! It works and now i finally understand! Thank you for your time
28th Jan 2023, 1:08 PM
Márton Boros
Márton Boros - avatar
+ 1
I did not checked it fully. I hope it it works but not sure. Because it may take much time. Simplified one, this works I think for all cases.. ".*(?=(.*[0-9]){2,}).*(?=(.*[#!@$%&*]){2,}).*" Still not checked for all. If anything not work, then add here. I try to look at later and reply. You're welcome..
28th Jan 2023, 1:24 PM
Jayakrishna 🇮🇳
0
Yes. Your regex matches for : pattern starts with atleast 2 digits in sequence followed by atleast 2 characters or at least 2 characters followed by atleast 2 digits in sequence ( not any order..). If you go on this way, it become lengthy but you can try it.. You need to make classes for regex. There is better ways to do. Previously, discussed about same task. Have a look at this.. https://www.sololearn.com/discuss/3083841/?ref=app edit: oh. not same but. once read this and try again.. hope it helps to get idea.
28th Jan 2023, 9:21 AM
Jayakrishna 🇮🇳