I have a situation where the end user is allowed to enter an arbitrary regex expression (php pcre). EG: ^[a-zA-Z]foo[0-9]{3}$
I need to ‘validate’ this regex both for:
- accuracy (that it will not cause an error when executed) and
- to ensure that the regex will not allow a string that matches the following pattern to be matched:
^[a-zA-Z0-9_\-]$
What is a good way (ideally in php) to perform the 2 validations above?
Note: I am not able to at runtime to simply use both regex expressions for matching each string.
That’s impossible. PCRE is a turing complete language, so in essence you’re trying to solve the halting problem. The only way to do it is to simply run the PCRE and check for errors.