Search code examples
phpregexpreg-matchsha1

regex php preg_match trying to validate string as sha1


I am trying to validate a string as sha1 with preg_match()and seem to be failing.

here is an example string of what I am looking for

$str = 'ad209b77134af9c598894a36ed46fc703a0c1e78';

here is the pattern I am using trying to get it to match, anyone with enough regex experience able to tell me whats wrong, cause I am horrible with regex.

$pattern = '#^[a-f0-9]{40}$#i';

then this is how I am using it

return (bool) preg_match($pattern, $str));

Solution

  • I found my error, its cause earlier in the function I am building, on that one particular rule I called "pattern", "patter" so it was causing the false outcome that was unexpected.