archmeta
archmeta

Reputation: 1127

How to validate a regex to ensure that it does NOT match another regex in php?

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:

  1. accuracy (that it will not cause an error when executed) and
  2. 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.

Upvotes: 0

Views: 126

Answers (1)

Sander Marechal
Sander Marechal

Reputation: 23216

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.

Upvotes: 7

Related Questions