I generate my own regex expression and I would like to check if it is valid. For example if I have regex "[a-zA-Z0-9]" it will be valid but "[a-zA-Z0s-9]" not.
1 Answer
To check if regex is valid from inside python:
import re
re.compile('<regex>')
This code will throw an error if it's invalid regex.
However, I find it much more useful to use regex tool, here's a few to choose from:
3 Comments
Honza
I want to catch invalid regex a generate my warning to stderr with exit code.
Honza
solved. thank you for help and hint! :)
Aaron Brock
You'll just need to surround it in a try except :)
re.compile()? You could do that in atry/exceptblock (and catchre.error).