Regex to Valid Password | SoloLearn: Learn to code for FREE!

+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])(?=.*[@#$&*!%]{2,}).{7,})"); Can any one tell me where is the problem in this code????

6/19/2020 4:15:20 PM

Sharmin Rumpa

24 Answers

New Answer

+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

+2

Anik Datta Right. But I wanna try this. This work just regular expressions is not correct. That's why some test are fail.

+2

Russ Thank you. I get it now.

+1

Or special characters?????

+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.

+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

+1

Russ thank now. I change the code, now code is Regex ("(?=(.*\\d){2,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$&*!%]{2,}).{7,}"); It's successful but test number 13 is fail

+1

$¢𝐎₹𝔭!𝐨𝓝 Thank you but I need to solve c# code

+1

$¢𝐎₹𝔭!𝐨𝓝 I saw your regex, your and my regex are same. It's work but text number 13 is fail. May be I somewhere

+1

Sharmin Rumpa I have solved it. You need (?=(.*\d){2,}) and (?=(.*[%*#[email protected]&]){2,} Group (.*[%*#[email protected]&]) needs to appear twice, not [%*#[email protected]&] twice (consecutively) after .*

+1

Copy and paste here, you have shared the link of the code coach

+1

$¢𝐎₹𝔭!𝐨𝓝 I try but I don't know how to share. Ok I copy it

+1

Sharmin Rumpa Your regex checks for two consecutive special characters, you need to change .*[%*#[email protected]&]{2,} to (.*[%*#[email protected]&]){2,}

+1

$¢𝐎₹𝔭!𝐨𝓝 https://code.sololearn.com/cit1hOf1gAy1/?ref=app

+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,})(?=(.*[@#$&*!%]){2,})(?=(.*[a-zA-Z]){3,}).{7,})"); Match m = password_pattern.Match(password); return m.Success; } } }

+1

Here are some regex solution for same tasks. https://www.sololearn.com/Discuss/2124383/?ref=app

+1

$¢𝐎₹𝔭!𝐨𝓝 Thank you, its work. But can you please explain where my mistake?

+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()))

0

Sharmin Rumpa , See regex is working fine. I have tested it. It should work with c# too.

0

$¢𝐎₹𝔭!𝐨𝓝 That doesn't explain why Sharmin Rumpa 's regex doesn't work. Would you be able to explain that?