I want to be able to check the user-entered regular expression to see if it is valid or not. The first thing I found with PHP was filter_var
with the constant FILTER_VALIDATE_REGEXP
, but that does not do what I want, since it should pass regex options, but I am not regex'ing against anything, so basically it's just checking the regular expression is correct.
But you understand how I can verify the correctness of the user-entered regular expression (which does not match anything).
An example of checking simple words:
$user_inputted_regex = $_POST['regex']; // eg /([az]+)\..*([0-9]{2})/i if(is_valid_regex($user_inputted_regex)) { // The regex was valid } else { // The regex was invalid }
Verification Examples:
/[[0-9]/i // invalid //(.*)/ // invalid /(.*)-(.*)-(.*)/ // valid /([az]+)-([0-9_]+)/i // valid
source share