0

Metacharacters

Hi so this is the question Can someone help to explain why my code wrong and correct it? Let's imagine we are creating our own authentication system. Create a program that takes a password as input and returns "Password created" if - it has at least one uppercase character - it has at least one number The Program should output "Wrong format" if requirements above are not met. Sample Input Hal44gb8 Sample Output Password created Code: import re password = input() #your code goes here pattern= r"(A-Z){1,},(a-z)*, (0-9){1,}" if re.match(pattern, password): print("Password created") else: print("Wrong format")

6th May 2021, 2:58 AM
Zhi Hong
Zhi Hong - avatar
11 Antworten
+ 5
I don't know of a way to combine regular expressions to ensure all of the expressions are found but the following code would get the behaviour you describe. With the commas, it looks like you want each and every pattern matched in parallel which I don't know how to do. The following will check that the provided string contains at least 1 upper case letter and 1 digit. I removed your reference to lower case letters (a-z)* completely because lower case letters were not mentioned in the requirements. import re password = input() #your code goes here patterns = [r"[A-Z]+", r"[0-9]+"] wrong_format = False for pattern in patterns: if not re.search(pattern, password): wrong_format = True break if wrong_format: print("Wrong format") else: print("Password created") Your attempt at the regular expression makes me think of slightly different requirements. If you want passwords to start with an upper case letter, contain a possibly empty sequence of lower case letters, and end with a single digit(ie. "Asdjh1" or "B5"), you could change your regular expression to r"^[A-Z][a-z]*[0-9]
quot; Here is complete code: import re password = input() pattern = r"^[A-Z][a-z]*[0-9]
quot; if re.match(pattern, password): print("Password created") else: print("Wrong format")
7th May 2021, 5:27 PM
Josh Greig
Josh Greig - avatar
+ 5
pattern = r"[A-Z]+[a-z]*[0-9]+"
14th May 2021, 12:22 PM
Volodymyr Shylov
Volodymyr Shylov - avatar
+ 3
import re Password = input() pattern = "[A-Z][a-z]*[0-9]" if re.match(pattern,Password): print("Password created") else: print("Wrong format")
16th Aug 2022, 3:17 PM
Saeed Karrabi
Saeed Karrabi - avatar
+ 1
import re password = input() #your code goes here pattern = r"[A-Z*][0-9]*" if re.match(pattern, password): print("Password created") else: print ("Wrong format")
16th Dec 2021, 11:37 PM
Adam Dirker
Adam Dirker - avatar
+ 1
import re password = input() #your code goes here May=r"[A-Z]" Num=r"[0-9]" if re.search(May,password): if re.search(Num,password): print("Password created") else: print("Wrong format")
28th May 2022, 6:25 PM
Jhonatan Pereda Azabache
+ 1
import re password = input() #your code goes here pattern = r".*[A-Z]+.*[0-9]+" #0+ character, 1+ Uppercase, 0+ character, 1+ digit if re.match(pattern, password): print("Password created") else: print ("Wrong format")
9th Jan 2024, 12:54 AM
K̅̈́̋͐͑̈́̓̚lí̞͕͓̬̞͛̒̄͂̄̍̋q-H̦̦̯͔̠̥̜̣̭͒̐̔̎̌̋͛̔́̇̎ͅir
K̅̈́̋͐͑̈́̓̚lí̞͕͓̬̞͛̒̄͂̄̍̋q-H̦̦̯͔̠̥̜̣̭͒̐̔̎̌̋͛̔́̇̎ͅir - avatar
0
3 import re password = input() #your code goes here pattern = r"[A-Z*][0-9]*" if re.match(pattern, password): print("Password created") else: print ("Wrong format")
23rd May 2022, 1:56 AM
Najwan Najmussabah
0
Let's imagine we are creating our own authentication system. Create a program that takes a password as input and returns "Password created" if - it has at least one uppercase character - it has at least one number The Program should output "Wrong format" if requirements above are not met. Sample Input Hal44gb8 Sample Output Password created
9th Aug 2022, 2:27 AM
Akanksha Pawar
0
import re password = str(input()) pattern1=r"[0-9]+" pattern2=r"[A-Z]+" if re.search(pattern1,password) and re.search(pattern2,password): print("Password created") else: print("Wrong format")
9th Feb 2023, 2:39 AM
Ata Taghipour
0
import re password = input() pattern = r".*[A-Z].*[0-9]+.*" if re.match(pattern, password): print("Password created") else: print("Wrong format")
5th Jun 2023, 7:21 PM
Yan
- 1
@Adam Dirker Thank you for your solution! I was curious, though. At first I tried "[A-Z]*[0-9]*" and some of the cases were wrong. However, when I switched it to "[A-Z*][0-9]*" like you had it, it worked. If you don't mind, could you explain the difference between the two? Why is it only the A-Z that has the asterisk inside the brackets and not the 0-9?
15th Feb 2022, 9:30 AM
Joseph Rowe
Joseph Rowe - avatar