Published on

Regex for MD5 string

Regex for MD5 string

In this article let's understand how we can create a regex for MD5 strings and how it can be matched.

MD5 (Message-Digest Algorithm 5) is a widely-used cryptographic hash function that generates a fixed-size, 128-bit (16-byte) output from any input data. It is commonly used to verify the integrity of data, such as files downloaded from the internet and to generate hash values for indexing or comparison purposes.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Let's look at regex expression for base64 strings. We will try a few approaches right from simple approach to RFC or advanced approach.

Regex to match MD5 string

Regular Expression for MD5 String can be represented as-


Alternatively you can use the i modifier in the regex takes care of the UPPERCASE letters.


Test string examples for the above regex-

Input StringMatch Output
ThisIsNotMD5does not match
zxcvbncvwfrqghdfjgyetwrteyrutituregdoes not match

Here is a detailed explanation of the above regex-


^ asserts position at start of a line
Match a single character present in the list below [a-fA-F0-9]
{128} matches the previous token exactly 128 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
/gm matches the characters /gm literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to match MD5 regex pattern.