Perl 正则表达式匹配 MD5 哈希值?
最近用 PHP 编程,我以为我有一个有效的 Perl 正则表达式,但是当我根据我想要的内容检查它时,它不起作用。
检查某物是否为 MD5 的正确表达式是什么(az 和 0-9 的 32 位十六进制数)。
目前,我有 /^[a-z0-9]{32}$/i
Recently programming in PHP, I thought I had a working Perl regular expression but when I checked it against what I wanted, it didn't work.
What is the right expression to check if something is a MD5 has (32 digit hexadecimal of a-z and 0-9).
Currently, I have /^[a-z0-9]{32}$/i
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
MD5:
SHA-1:
MD5 或 SHA-1:
此外,大多数哈希值始终以小写形式表示十六进制方式,因此您可能需要考虑删除
i
修饰符。顺便说一句,十六进制表示基数为 16:
因此,如您所见,它仅从 0 到 F,同样,十进制(或基数 10)仅从 0 到 9。
MD5:
SHA-1:
MD5 or SHA-1:
Also, most hashes are always presented in a lowercase hexadecimal way, so you might wanna consider dropping the
i
modifier.By the way, hexadecimal means base 16:
So as you can see it only goes from 0 to F, the same way decimal (or base 10) only goes from 0 to 9.
应该会工作得更好一些,因为 MD5 哈希值通常表示为十六进制数字。
Should work a bit better, since MD5 hashes usually are expressed as hexadecimal numbers.
还有 POSIX 字符类
xdigit
(请参阅 perlreref):There is also the POSIX character class
xdigit
(see perlreref):嗯,需要考虑的重要一点是
$
可以匹配\n
。因此:哎呀!
因此,正确的模式是:
Well, an important point to consider is the fact that
$
can match\n
. Therefore:Ooops!
The correct pattern, therefore, is:
甚至比 PHP Ctype 函数 推荐的正则表达式更简单、更快:
Even easier and faster than RegEx as recommended by PHP Ctype Functions :
@OP,您可能想要使用
/[a-f0-9]{32,40}/
,这可以检查长度是否大于 32,例如从 sha1 生成的长度。@OP, you might want to use
/[a-f0-9]{32,40}/
, this can check for length greater than 32, such as those generated from sha1.