user1702396 user1702396 - 11 months ago 47
PHP Question

How to check if a given string is valid Regex?

Possible Duplicate:

Test if a regular expression is a valid one in PHP


$subject = "PHP is the web scripting language of choice.";
$pattern = 'sssss';

echo 'true';
echo 'false';


The above code gives me warning because the string
is not a valid regular expression.

If I pass valid regular expression then it works fine.....

How can i check
is valid regular expression?


You can just wrap preg_match with try catch, and consider result false, if it throws exception.

Anyway, you can take a look at regular expression to detect a valid regular expression.