Reputation: 5
I want to validate an 8 character string with the following position-wise character constraints:
A/M/P/B/S
AA
to ZZ
1
- 4
1
- 4
000
- 999
If it doesn't match, it will return FALSE
. Here is my code:
import re
text = input("input:")
print(text)
check = re.search("[^S|B|M|P|A][AA-ZZ][1-4][1-4][000-999]", text)
if check:
print("TRUE")
else:
print("FALSE")
Ex : AIK22001 The Uotput should be 'True'
Here, I have tried using regex Python library, but the output was wrong. It returns False
whether it is expected to be True
.
Upvotes: 0
Views: 1571
Reputation: 10414
Here's an example of how to use the regex101 site that Akshay Sehgal mentioned above:
https://regex101.com/r/KtzJY6/1
You'll see the regex there is ^[AMPBS][A-Z]{2}[1-4]{2}[0-9]{3}$
. Important features to note here:
^
and $
attach to the beginning and ending of the string, which prevents inputs that are too short and too long from matching.{2}
, which means "two of the previous things"[]
work. You don't need |
in the middle of them, and ranges only work on single characters; [AA-ZZ]
matches 'A', anything between 'A' and 'Z', and 'Z', which is probably not what you wanted.Upvotes: 1