user1702396 user1702396 - 1 year ago 105
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download