Let’s say you need to validate strings in Python. Making decisions if a string is valid or not, is what we will be looking at today.
The Scenario
We have a string that will look like this: my-random-abc-string-2947104284738593726152637836291
. The abc
section will always be 3 random string characters and the integers, will always be 32 integer characters, the rest will always stay the same.
Using the re
library, we will create our regex expression and match them up with a input string, then if they are the same, we will pass the validation check, and make a decision from there.
The Script
Our random string generator:
1 2 3 4 5 6 7 |
|
Our validation check:
1 2 3 4 5 |
|
Doing the validation check against our data:
1 2 3 4 5 6 7 8 9 10 11 |
|
The function checks for a strict 32 characters in the random hex number, if you had to randomize the length, you can always use this regex:
1
|
|