Test if a string is regex

后端 未结 4 1716
遥遥无期
遥遥无期 2020-12-03 13:50

Is there a good way of test if a string is a regex or normal string in PHP?

Ideally I want to write a function to run a string through, that returns true or false.

相关标签:
4条回答
  • 2020-12-03 14:13

    Why not just use...another regex? Three lines, no @ kludges or anything:

    // Test this string
    $str = "/^[A-Za-z ]+$/";
    
    // Compare it to a regex pattern that simulates any regex
    $regex = "/^\/[\s\S]+\/$/";
    
    // Will it blend?
    echo (preg_match($regex, $str) ? "TRUE" : "FALSE");
    

    Or, in function form, even more pretty:

    public static function isRegex($str0) {
        $regex = "/^\/[\s\S]+\/$/";
        return preg_match($regex, $str0);
    }
    

    This doesn't test validity; but it looks like the question is Is there a good way of test if a string is a regex or normal string in PHP? and it does do that.

    0 讨论(0)
  • 2020-12-03 14:18

    The simplest way to test if a string is a regex is:

    if( preg_match("/^\/.+\/[a-z]*$/i",$regex))
    

    This will tell you if a string has a good chance of being intended to be as a regex. However there are many string that would pass that check but fail being a regex. Unescaped slashes in the middle, unknown modifiers at the end, mismatched parentheses etc. could all cause problems.

    The reason preg_last_error returned 0 is because the "invalid regex" is not:

    • PREG_INTERNAL_ERROR (an internal error)
    • PREG_BACKTRACK_LIMIT_ERROR (excessively forcing backtracking)
    • PREG_RECURSION_LIMIT_ERROR (excessively recursing)
    • PREG_BAD_UTF8_ERROR (badly formatted UTF-8)
    • PREG_BAD_UTF8_OFFSET_ERROR (offset to the middle of a UTF-8 character)
    0 讨论(0)
  • 2020-12-03 14:21

    Here is a good answer how to:

    https://stackoverflow.com/a/12941133/2519073

    if(@preg_match($yourPattern, null) === false){
        //pattern is broken
    }else{
        //pattern is real
    }
    
    0 讨论(0)
  • 2020-12-03 14:25

    The only easy way to test if a regex is valid in PHP is to use it and check if a warning is thrown.

    ini_set('track_errors', 'on');
    $php_errormsg = '';
    @preg_match('/[blah/', '');
    if($php_errormsg) echo 'regex is invalid';
    

    However, using arbitrary user input as a regex is a bad idea. There were security holes (buffer overflow => remote code execution) in the PCRE engine before and it might be possible to create specially crafted long regexes which require lots of cpu/memory to compile/execute.

    0 讨论(0)
提交回复
热议问题