Regex to Valid Password | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Regex to Valid Password

The password should have at least 2 number & 2 special characters & minimum 7 characters. My Ragex is here : Regex ("(?=(.*\d){2,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#

amp;*!%]{2,}).{7,})"); Can any one tell me where is the problem in this code????

19th Jun 2020, 4:15 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
24 Answers
+ 2
Sharmin Rumpa sorry for my first comment. You can make 2 array one for numbers and one for special characters then check and count them from your password string
19th Jun 2020, 4:37 PM
Anik Datta
Anik Datta - avatar
+ 2
Anik Datta Right. But I wanna try this. This work just regular expressions is not correct. That's why some test are fail.
19th Jun 2020, 4:41 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 2
Russ Thank you. I get it now.
19th Jun 2020, 7:23 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 1
Or special characters?????
19th Jun 2020, 4:25 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 1
Assuming you're attempting the Password Validation Code Coach task, the description doesn't mention there has to be any letters (upper or lower case) in the password. I'm not saying this is the problem, but it could be that. Also, I think you have an extra ")" at the end of your regex string.
19th Jun 2020, 5:13 PM
Russ
Russ - avatar
+ 1
Hello Sharmin Rumpa , Here is my try. Check and let me know if you need some changes. https://code.sololearn.com/c4DyMG8nQvA7/?ref=app
19th Jun 2020, 6:35 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
+ 1
Russ thank now. I change the code, now code is Regex ("(?=(.*\\d){2,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#
amp;*!%]{2,}).{7,}"); It's successful but test number 13 is fail
19th Jun 2020, 6:35 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 1
$ยข๐Žโ‚น๐”ญ!๐จ๐“ Thank you but I need to solve c# code
19th Jun 2020, 6:41 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 1
$ยข๐Žโ‚น๐”ญ!๐จ๐“ I saw your regex, your and my regex are same. It's work but text number 13 is fail. May be I somewhere
19th Jun 2020, 6:54 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 1
Sharmin Rumpa I have solved it. You need (?=(.*\d){2,}) and (?=(.*[%*#$!@&]){2,} Group (.*[%*#$!@&]) needs to appear twice, not [%*#$!@&] twice (consecutively) after .*
19th Jun 2020, 6:58 PM
Russ
Russ - avatar
+ 1
Copy and paste here, you have shared the link of the code coach
19th Jun 2020, 7:08 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
+ 1
$ยข๐Žโ‚น๐”ญ!๐จ๐“ I try but I don't know how to share. Ok I copy it
19th Jun 2020, 7:09 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 1
Sharmin Rumpa Your regex checks for two consecutive special characters, you need to change .*[%*#$!@&]{2,} to (.*[%*#$!@&]){2,}
19th Jun 2020, 7:12 PM
Russ
Russ - avatar
+ 1
Sharmin Rumpa , try this using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace SoloLearn { class Program { static void Main(string[] args) { string password = Console.ReadLine(); Console.WriteLine(validate(password)? "Strong" : "Weak"); } static bool validate(string password) { Regex password_pattern = new Regex ("((?=(.*\\d){2,})(?=(.*[@#
amp;*!%]){2,})(?=(.*[a-zA-Z]){3,}).{7,})"); Match m = password_pattern.Match(password); return m.Success; } } }
19th Jun 2020, 7:18 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
+ 1
Here are some regex solution for same tasks. https://www.sololearn.com/Discuss/2124383/?ref=app
19th Jun 2020, 7:21 PM
Preity
Preity - avatar
+ 1
$ยข๐Žโ‚น๐”ญ!๐จ๐“ Thank you, its work. But can you please explain where my mistake?
19th Jun 2020, 7:22 PM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 1
Here is a example in python import re def password_validation(password): numbers = re.findall(r'[0-9]', password) specialChars = re.findall(r'\W', password) if len(password) > 6 and len(numbers) > 1 and len(specialChars) > 1: return 'Strong' return 'Weak' print(password_validation(input()))
20th Jun 2020, 3:54 AM
Jonathan Alvarado
Jonathan Alvarado - avatar
0
Sharmin Rumpa , See regex is working fine. I have tested it. It should work with c# too.
19th Jun 2020, 6:46 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
0
$ยข๐Žโ‚น๐”ญ!๐จ๐“ That doesn't explain why Sharmin Rumpa 's regex doesn't work. Would you be able to explain that?
19th Jun 2020, 6:51 PM
Russ
Russ - avatar