Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Regular Expressions to match a MD5 Hash?

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

like image 575
Eric Gates Avatar asked Feb 18 '10 01:02

Eric Gates


People also ask

What is \d in Perl regex?

The Special Character Classes in Perl are as follows: Digit \d[0-9]: The \d is used to match any digit character and its equivalent to [0-9]. In the regex /\d/ will match a single digit. The \d is standardized to “digit”.

How do I match a new line in a regular expression in Perl?

Use /m , /s , or both as pattern modifiers. /s lets . match newline (normally it doesn't). If the string had more than one line in it, then /foo. *bar/s could match a "foo" on one line and a "bar" on a following line.

Which function is used for Perl Compatible regular expression?

As of Perl 5.10, PCRE is also available as a replacement for Perl's default regular-expression engine through the re::engine::PCRE module. The library can be built on Unix, Windows, and several other environments.


1 Answers

MD5:

/^[0-9a-f]{32}$/i

SHA-1:

/^[0-9a-f]{40}$/i

MD5 or SHA-1:

/^[0-9a-f]{32}(?:[0-9a-f]{8})?$/i

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:

0  1  2  3  4  5  6  7  8  9  A   B   C   D   E   F  = base 16
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 = base 10

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.

like image 78
Alix Axel Avatar answered Oct 12 '22 18:10

Alix Axel