可能重复:
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';
}
?>
上面的代码给我警告,因为串$pattern
不是有效的正则表达式。
如果我通过有效的正则表达式,然后它工作正常.....
我怎么能检查$pattern
是有效的正则表达式?
在这里看到正确的:http://stackoverflow.com/questions/7095238/an-invalid-regex-pattern –
,或者更好,这里:http://stackoverflow.com/questions/362793/regexp-that-matches-valid-regexps – k102
或这一个:http://stackoverflow.com/questions/8825025/test-if-a-regular-expression-是一个有效的一个在PHP –