I want to make sure using regex that a string is of the format- “999.999-A9-Won” and without any white spaces or tabs or newline characters.
- There may be 2 or 3 numbers in the range 0 – 9.
- Followed by a period ‘.’
- Again followed by 2 or 3 numbers in the range 0 – 9
- Followed by a hyphen, character ‘A’ and a number between 0 – 9 .
- This can be followed by anything.
Example: 87.98-A8-abcdef
The code I have come up until now is:
testString = "87.98-A1-help"
regCompiled = re.compile('^[0-9][0-9][.][0-9][0-9][-A][0-9][-]*');
checkMatch = re.match(regCompiled, testString);
if checkMatch:
print ("FOUND")
else:
print("Not Found")
This doesn’t seem to work. I’m not sure what I’m missing and also the problem here is I’m not checking for white spaces, tabs and new line characters and also hard-coded the number for integers before and after decimal.
With
{m,n}you can specify the number of times a pattern can repeat, and the\dcharacter class matches all digits. The\Scharacter class matches anything that is not whitespace. Using these your regular expression can be simplified to:Note also the
\Zanchor, making the\S*expression match all the way to the end of the string. No whitespace (newlines, tabs, etc.) are allowed here. If you combine this with the.match()method you assure that all characters in your tested string conform to the pattern, nothing more, nothing less. Seesearch()vs.match()for more information on.match().A small demonstration: