1

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.

3
  • 3
    Using re.compile()? You could do that in a try/except block (and catch re.error). Commented Mar 29, 2017 at 21:15
  • Also, what do you mean by "generate my own regex"? Are you dynamically generating regexes? Commented Mar 29, 2017 at 21:32
  • Yes... I generate own regexes according to some pattern. But sometimes it makes mistakes, so I want to catch that a generate my warning to stderr with exit code. Commented Mar 29, 2017 at 21:47

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

3 Comments

I want to catch invalid regex a generate my warning to stderr with exit code.
solved. thank you for help and hint! :)
You'll just need to surround it in a try except :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.