md5 regex patterns

Regex Patterns Examples to Match MD5 Hashes in any Language

If you are a developer, you know that regex are really useful to find or check something quickly. But usually, they are not really the easiest thing to do, especially if you are not sure of what you are looking for. That’s why I wrote this article, to give you the guidelines but also a few examples, so that you can do it easily in any language.

As a general rule, MD5 hashes are always a string of 32 characters composed of letters and numbers. So, the corresponding regex pattern should be “/^[A-F0-9]{32}/i”.

In this article, we’ll see how to implement this solution in most languages. So, it will be just a copy and paste to use it in your application.

Master Linux Commands
Your essential Linux handbook
Want to level up your Linux skills? Here is the perfect solution to become efficient on Linux. 20% off today!

Download now

Regex to match a MD5 in PHP

Master your cyber security skills:
Secure your spot in the Accelerator Program, with early access to exclusive resources.
Get 1000+ classes, unlimited mentorship, and more.

PHP is one of the easiest languages to use, and the regex functions are directly available. The main function to use regex in PHP are preg_match() and preg_match_all(). You don’t need any additional module to use it in your code.

The syntax is the following:
preg_match( $pattern, $subject, $matches);
The last argument is optional (if you just want to check that $subject is a MD5 hash, you don’t need it.
preg_match_all works has the same parameters. The only difference is that it will return all the corresponding hashes (not just the first one).

Here is an example with preg_match:

<?php
$string = "d3c8e06e57cc1af7ebdba01427e62bc2";
if(preg_match('/[0-9a-f]{32}/i', $md5)) {
   echo "This is a MD5 hash";
}
else {
   echo "This isn't a valid MD5 hash";
}
?>
Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!

That’s it, pretty simple.

MD5 regex pattern in Python

In Python, you need to import a module (“re”) and the two functions work a bit differently.
It’s not complicated either but the two functions are:

  • var = re.search($pattern,$subject)
  • var = re.findall($pattern,$subject)

Here is a complete example with the two functions in Python:

import re

string = "d3c8e06e57cc1af7ebdba01427e62bc2";
result1 = re.search("^[0-9a-fA-F]{32}$",string)
print(result1.string)

result2 = re.findall("^[0-9a-fA-F]{32}$",string)
print(result2)

result1 is an object while result2 is a string (a list), so you need to use them differently in your code.

Please note that I changed the regex a bit to be case-insensitive, but you can also add an argument : “re.IGNORECASE”, like this:
result2 = re.findall("^[0-9a-f]{32}$",string,re.IGNORECASE)

Java examples of MD5 regex patterns

Java is never the easiest language to use (or maybe it’s just me ^^), but you can also check MD5 hashes pretty easily by using the “matches” function on a string variable.

Here is an example:

public static void main(String args[]) {

    String hash = "d3c8e06e57cc1af7ebbba01427e62bc2";
    if(hash.matches("(?i)^[a-f0-9]{32}$")) {
        System.out.println("This is a MD5");
    }
    else {
        System.out.println("Not a MD5");
    }

}

Regex patterns to catch MD5 hashes in C / C++ / C#

The last language I think might be useful is C. But I think you already understood the idea with the other languages.
It’s the most complicated in my list I think, but if you are a good C developer, it should be ok.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!

The first thing to do to match MD5 patterns in C is to include the regex library (regex.h).
Then the usage is a bit weird, as you first need to compile your regular expression with “regcomp” before executing it with regexec.

Anyway, here is a basic example, so that it will be clearer:

include <stdio.h>
include <string.h>
include <regex.h>

int main ()
{
   char *string = "d3c8e06e57cc1af7ebdba01427e62bc2";
   char *regexString = "[0-9a-f]{32}";
   regex_t regexCompiled;
   unsigned int m;

   if (regcomp (&regexCompiled, regexString, REG_EXTENDED+REG_ICASE))
   {
      printf ("Could not compile regular expression.\n");
      return 0;
   };

   m = regexec (&regexCompiled, string, 0, NULL, 0);
   if (!m)
   {
      printf ("Match\n");
      return 1;
   }
}

How to catch a MD5 hash on Linux?

Finally, I want to show you how to match MD5 hashes on Linux, in a file for example.

Hide your IP address and location with a free VPN:
Try it for free now, with advanced security features.
2900+ servers in 65 countries. It's free. Forever.

There are several solutions to match a MD5 on Linux, but the easiest one will be to use the command “grep”. By default, grep is allowing us to use regex, and there are a few options to adjust the output string.

Here are two examples you can try:

  • If you want to catch the lines containing a valid MD5 hash, you can use:
    grep -i "[0-9a-f]{32}" yourfile.txt
    The -i parameter add a case-insensitive match.
  • If you only want the MD5 hash, here is the option:
    grep -ohi "[0-9a-f]{32}" yourfile.txt
    -o stands for “only matching”, that’s why it displays only the MD5 hash
    -h will remove the filename from the output

Other methods in Linux could be to use more advanced commands like awk or sed, but if you just need to find MD5 in a file, grep is the easiest way.

I hope you have found the answer you were looking for in this article.
Basically, the regex pattern is always the same, we just need to adjust the code around it for each language.

If you are a developer or like to code, you’ll probably love this other articles :

Whenever you’re ready for more security, here are things you should think about:

- Break free from Gmail: You should be able to choose what happens to your data. With Proton, only you can read your emails. Get private email.

- Protect yourself online: Use a high-speed Swiss VPN that safeguards your privacy. Open-source, no activity logs. Get Proton VPN risk-free.

- Master Linux commands: A sure method to learn (and remember) Linux commands. Useful ones only, one at a time, with clear explanations. Download the e-book.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *