Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^[a-fA-F0-9]{64}$

Matches a string that is exactly 64 characters long, consisting only of hexadecimal digits (case-insensitive).

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string '' (empty string at start)
[ Start of a character class N/A
a-f Range: matches any lowercase letter from 'a' to 'f' 'a', 'b', 'f'
A-F Range: matches any uppercase letter from 'A' to 'F' 'A', 'B', 'F'
0-9 Range: matches any digit from '0' to '9' '0', '5', '9'
] End of a character class N/A
{64} Quantifier: exactly 64 occurrences of the preceding element (the character class) 64 hex digits in sequence
$ Anchor: asserts position at end of the string '' (empty string at end)

Matches

  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
  • ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789
  • aB3fE5d7c9A1b2C4e6F8g0H
  • 0000000000000000000000000000000000000000000000000000000000000000

Does not match

  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde
  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefg
  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0

Common pitfalls

  • Forgetting the anchors (^ and $) may allow strings with extra characters to match.
  • Using a case-insensitive flag (e.g., re.IGNORECASE) is redundant here since the pattern already includes both cases.
  • Assuming the pattern matches any hex string of length 64 or less (it must be exactly 64).
  • Confusing hexadecimal ranges (e.g., including 'g-z' or 'G-Z' would cause incorrect matches).

Performance notes

This pattern is efficient with no risk of catastrophic backtracking due to the fixed quantifier and simple character class. Anchors ensure the entire string is checked in one pass.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this pattern match both uppercase and lowercase hex digits?

Yes, the pattern explicitly includes both ranges [a-f] and [A-F].

Can I use this pattern to validate a SHA-256 hash?

Yes, SHA-256 hashes are 64-character hexadecimal strings, so this pattern is suitable for validation.

What happens if I remove the anchors (^ and $)?

The pattern would match any substring of 64 hex digits within a longer string, not necessarily the entire string.

Is there a more concise way to write this pattern?

Yes, in Python you could use the re.IGNORECASE flag and write r'^[a-f0-9]{64}$', but the original pattern is already clear and explicit.