+ 1
Trying to solve online shop in python core
import re Id = input() pattern=r"[A-Z][A-Z][0-9][0-9]" if re.search(pattern,Id): print("Searching") else: print("Wrong format") One test case out of 5 seem to come out wrong and i don't know why All the products in online shop have their own ID. Every ID consists of 4 symbols: The first symbol: an uppercase character The second symbol: an uppercase character The third symbol: a digit The forth symbol: a digit Write a program for a search tool, that will take the ID as input and output "Searching" if the format is correct, and "Wrong format", if it's not. Sample Input LG17 Sample Output Searching
11 Respuestas
+ 10
import re
Id = input()
pattern = r"^[A-Z][A-Z][0-9][0-9]quot;
if re.search(pattern, Id):
print('Searching')
else:
print('Wrong format')
#the Code is Correct
+ 5
Simply check the length of it .
Or you can use a pattern like this : ^[A-Z][A-Z][0-9][0-9]$
Take a look at the following article if you don't know how "^" and "quot; Symbol works.
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/regex
+ 3
I can't test it but try checking for length of the inputted id as well since re.search will match those 4 symbols even in a input like "4ZA56" or "ZA567"
+ 2
Okay
So how do i restrict it to checking 4 symbols
+ 1
Thanks
+ 1
import re
Id = input()
#your code goes here
pattern = r"^[A-Z][A-Z][0-9][0-9]quot;
if re.search(pattern, Id):
print('Searching')
else:
print('Wrong format')
+ 1
import re
Id = input()
pattern = r"^[A-Z][A-Z][0-9][0-9]quot;
if re.search(pattern, Id):
print('Searching')
else:
print('Wrong format')
0
import re
Id = input()
pattern=r"\b[A-Z]{2}[0-9]{2}\b"
element=re.findall(pattern,Id)
if element :
print("Searching")
else:
print("Wrong format")
print (Id)
print (re.findall(pattern,Id))
0
just a minor change =>if re.search(pattern,str(Id)):
and it works
0
import re
Id = input()
pattern = r"['A-Z']['A-Z']['0-9']['0-9']"
if len(Id)>4:
print("Wrong format")
elif re.search(pattern, Id):
print("Searching")
else:
print("Wrong format")
#this will work i think
0
import re
Id = input()
if re.search(r"[A-Z][A-Z][0-9][0-9]quot;, Id):
print("Searching")
else:
print("Wrong format")
Элементарно ватсон)