-2

I am new to Python and I want to write a pattern(single pattern if possible) which meets the below conditions,can anyone help me please

Following are the criteria for checking the password:

At least 1 letter between [a-z] At least 1 number between [0-9] At least 1 letter between [A-Z] At least 1 character from [$#@] Minimum length of transaction password: 6 Maximum length of transaction password: 12 Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.

Example

If the following passwords are given as input to the program:

ABd1234@1,a F1#,2w3E*,2We3345

Then, the output of the program should be:

ABd1234@1

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
stranger
  • 9
  • 3
  • That doesn't sound like a good job for a single regular expression. It would get very complex. – Klaus D. Jul 18 '20 at 04:29
  • @KlausD., I can think of reasons why one might choose to not use a regular expression for this task, but the regular expression needed, though requiring for than a few characters, is not really complex. Each of the requirements can be dealt with independently and is easily implemented, as you can see from Tim's answer. – Cary Swoveland Jul 18 '20 at 04:44
  • Don't get me wrong, stranger, as @Tim has given an excellent answer, but in future please consider waiting longer before selecting an answer. Quick selections can discourage other answers, may result in incorrect answers going unchecked by readers and may not be appreciated by those still working on their answers. There is no rush. Many askers wait at least a couple of hours, some much longer. Just don't forget to select an answer if at least one was helpful to you. – Cary Swoveland Jul 18 '20 at 04:49
  • I am really sorry @CarySwoveland , I am new to stackoverflow and I thought that the question automatically closes when someone gives you the right answer but now that you told me the way it works I will definitely follow your suggestion in the future.Thank you :) – stranger Jul 18 '20 at 04:59
  • 1
    I suppose the requirements are somewhat clear, but do you really want to allow other used characters to be of any type? Or are there restrictions on which symbols can be used too? I guess what I'm trying to say, currently you would accept `A...b||0 @` – JvdV Jul 18 '20 at 05:51
  • @JvdV, or `A...¯\_(ツ)_/¯ @`. – Cary Swoveland Jul 18 '20 at 06:13
  • Egad, don't be really sorry, or even just normal sorry. I did not mean it as a rebuke. I saw you were new to SO (welcome, btw), and was just suggestion you think about that. Not everyone agrees with me on this, as many SO veterans think quick-draw selections are just fine. When you have the time I suggest you read over what amounts to the [SO faq](https://stackoverflow.com/help). – Cary Swoveland Jul 18 '20 at 06:21

1 Answers1

1

The regex pattern you want for a single password is:

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[$#@]).{6,12}$

This uses three positive lookaheads to assert that a lowercase letter, an all caps letter, digit, and special character, are all present. Then, it matches any 6-12 characters.

If you wanted to extract such matching passwords from a CSV string, you could use re.findall:

inp = "ABd1234@1,a F1#,2w3E*,2We3345"
matches = re.findall(r'(?<![^,])(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[$#@])[^,]{6,12}(?![^,])', inp)
print(matches)

This prints:

['ABd1234@1']

Note that I have slightly modified the regex pattern being used with re.findall to not allow commas to be part of the password. This is necessary because otherwise we don't know if a comma be a separator between passwords or actually part of the password itself.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360