Suppose that a bank only permits passwords that are strings from the alphabet = {a, b, c, d, 1, 2, 3, 4} that follow the rules:

 The length is at least five characters

 It begins with a letter {a, b, c, d}

 It ends with two digits {1, 2, 3, 4}

The set of legal passwords forms a regular language L. Construct a regular expression r such that L(r) = L.

Respuesta :

Answer:

#Python

import re

password = input("Enter password: ")

p = re.compile('^[ABCDabcd]+.*[^e-zE-Z5-9_]{2,}[1-4][1-4]+$')

match = p.search(password)

if match == None:

    print('L(r) != L -> Incorrect password')

else:

    print('L(r) = L -> Correct password')

Explanation:

The regular expression needed is:

^[ABCDabcd]+.*[^e-zE-Z5-9_]{2,}[1-4][1-4]+$

To understand why see the step by step of the regex:

  1. ^[ABCDabcd]: Here we use ^ to indicate the first character of the class, then we use [] to specify a character class, then we punt ABCDabcd to indicate that the first character has to begin with a letter from a to d and finally we use a + to continue our expression
  2. .*[^e-zE-Z5-9_]{2,}: Here we use .* to specify the sequence of characters that comes after the first letter, then we put ^e-zE-Z5-9_ inside brackets, the first character, ^, tell us that the characters coming are forbidden, so the user can not use a number, letter or special character outside of the sequence defined, and finally, we have curly brackets to indicate the length of the expression, where '2,' represents that the length in the middle is at least two characters long
  3. [1-4][1-4]+$: With the two brackets in this expression the string must ends with two digits from one to fourth and the $ refers to the end of the expression

Note: if you don't have python you can use an online regex checker like myregextester, note that the string must be at least five characters long because you need one letter at the beginning, at least two characters in the middle and two numbers at the end.

RELAXING NOICE
Relax