Regex to match md5 hashes

Regex to match md5 hashes

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Regex to match MD5 hashes

Validating an MD5 hash requires understanding both the character constraints and the fixed length of the string. The example you provided, 00236a2ae558018ed13b5222ef1bd987, is a classic 32-character hexadecimal string. To match this pattern, you need a regular expression that enforces two strict rules: the presence of only lowercase alphanumeric characters and a precise length of exactly 32 characters.

A simple check like /^[a-z0-9]/ only verifies that the string starts with a valid character; it does not validate the rest of the string or its total length. This is why your initial attempt failed to correctly identify MD5 hashes. A robust pattern must anchor the start and end of the string while defining the character set for every position in between.

The correct regex pattern is:
/^[a-f0-9]{32}$/i

Here is a breakdown of why this specific pattern works:

  • ^: Anchors the match to the beginning of the string. Without this, the regex might match a substring within a longer text.
  • [a-f0-9]: Defines the allowed character set. An MD5 hash consists strictly of hexadecimal digits (0–9 and a–f). Including numbers 1–9 covers the numeric part, while a-f covers the letters. Note that we explicitly exclude g through z, as well as uppercase letters, unless specified otherwise.
  • {32}: Enforces the exact length. An MD5 hash is always 32 characters long. This prevents false positives from SHA-1 hashes (which are 40 characters) or truncated strings.
  • $: Anchors the match to the end of the string, ensuring no extra characters follow.
  • i: The case-insensitive flag allows the pattern to match both lowercase and uppercase hex digits (e.g., A instead of a).

If you are working within a Laravel application, you can apply this validation logic directly in your code. While Laravel provides powerful built-in validators for many data types, custom regex is necessary for specific hash formats like MD5 when standard rules don't apply. You can define a custom validation rule using the regex method:

use Illuminate\Support\Facades\Validator;

$validator = Validator::make(
    ['hash' => '00236a2ae558018ed13b5222ef1bd987'],
    [
        'hash' => 'required|string|regex:/^[a-f0-9]{32}$/i'
    ]
);

if ($validator->fails()) {
    echo $validator->errors()->first();
}

This approach ensures that the input is not only a string but also strictly adheres to the MD5 specification. Attempting to use generic hexadecimal validation without the length constraint {32} can lead to security vulnerabilities if you are inadvertently accepting shorter hashes or hashes from other algorithms like SHA-256.

One common pitfall when implementing this logic is ignoring case sensitivity. If your application receives uppercase hashes (e.g., A0...), a pattern without the i flag will reject them, even though they represent the same hash value. Always ensure your validation rules account for the specific casing expected by your backend API or database schema.

Furthermore, remember that regex is a tool for format validation, not security verification. Matching an MD5 hash with regex confirms the string looks like a hash, but it does not verify if that hash corresponds to a valid password or file integrity. For actual cryptographic verification, you must use hashing libraries rather than relying solely on pattern matching.

Our network