1

Possible Duplicate:
Test if a regular expression is a valid one in PHP

 <?php 

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

    if(preg_match($pattern,$subject))
    {
        echo 'true';
    }
    else
    {
        echo 'false';
    }

?>

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

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

How can i check $pattern is valid regular expression?

Community
  • 1
  • 1
user1702396
  • 145
  • 1
  • 2
  • 9

3 Answers3

5

You could write a function that throws an error if there is something wrong with the Regexp. (Like it should have been in my opinion.) It is bad practice to use the @ to suppress warnings, but if you replace it with a thrown exception it should be ok.

function my_preg_match($pattern,$subject)
{
    $match = @preg_match($pattern,$subject);

    if($match === false)
    {
        $error = error_get_last();
        throw new Exception($error['message']);
    }
    return false;
}

then you can check if the regexp is correct with

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

try
{
    my_preg_match($pattern,$subject);
    $regexp_is_correct = true;
}
catch(Exception $e)
{
    $regexp_is_correct = false;
}
Archaeron
  • 767
  • 1
  • 7
  • 15
0

Use the === operator:

<?php 

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

    $r = preg_match($pattern,$subject);
    if($r === false)
    {
        // preg matching failed (most likely because of incorrect regex)
    }
    else
    {
        // preg match succeeeded, use $r for result (which can be 0 for no match)
        if ($r == 0) {
            // no match
        } else {
            // $subject matches $pattern
        }
    }

?>
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • I fixed the syntax error (case of copy-paste mistake). And my answer gives a way of detecting if the regex is incorrect (which is was the OP asked for). – Bart Friederichs Jan 10 '13 at 08:35
  • @loler how exactly? `preg_match($pattern, $subject);` is perfectly valid PHP. – Bart Friederichs Jan 10 '13 at 08:37
  • @loler apparently you are misreading the OP's question. He **says** the regex is invalid. This is not a syntax error, it's a warning. – Bart Friederichs Jan 10 '13 at 08:43
  • @loler no. OP's code also drops in the "false" part if there is no match. Which is something else than a regex error. Check this: http://nl3.php.net/manual/en/language.operators.comparison.php and the remark on preg_match's documentation page: "This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE." – Bart Friederichs Jan 10 '13 at 08:55
-1

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.

Community
  • 1
  • 1
loler
  • 2,594
  • 1
  • 20
  • 30