Natural Language Processing · TEE · papers
Q5a · Password regex
2023-01-30 · re-exam · recovered AI paper
Previous examination · complete worked answers
Question. Write a regex for an 8–15 character password containing letters and digits, at least one uppercase letter and at least one special character from !%@*, with no other characters. [10 marks]
Answer
^(?=.{8,15}$)(?=.*[A-Z])(?=.*[0-9])(?=.*[!%@*])[A-Za-z0-9!%@*]+$
| Part | Purpose |
|---|---|
| (?=.{8,15}$) | Length8–15 |
| (?=.*[A-Z]) | At least one uppercase; this also meets the letter requirement |
| (?=.*[0-9]) | At least one digit |
| (?=.*[!%@*]) | At least one permitted special |
| [A-Za-z0-9!%@*]+ | Only permitted characters, including lowercase if desired |
Use full-string matching. Abcdef1! passes; abcdef1! fails uppercase, Abcdefgh fails digit/special, Abc1! is too short, Abcdef1# contains forbidden #. A lowercase character is not separately required by the printed conditions.