mercredi 17 février 2021

How to validate string using regex

I want to validate an 8 character string.

The first position must be one of these letters: A/M/P/B/S

Second and third position are letters from AA to ZZ

Fourth position contains a number between 1 - 4

Fifth position contains a number between 1 - 4

Sixth - eighth position must be a number 000 - 999

If it doesn't match, it will return FALSE.

import re                       

text = input("input:")
print(text)
if len(text) != 8 :
 print("FALSE")
 check = re.search("[^S|B|M|P|A][AA-ZZ][1-4][1-4][000-999]", text)
elif check: 
 print("TRUE")
else: 
 print("FALSE")

I have tried using regex Python library like this, but the output was wrong. It returns False when it should be True.

Aucun commentaire:

Enregistrer un commentaire