REGEXVAULTv2.0
Finance/Crypto
Verified Safe

Crypto Transaction Hash (Generic) Regex for Java

/^(?:0x)?[0-9a-fA-F]{64}$/

What this pattern does

This page provides a lightweight, single-purpose regular expression for matching crypto transaction hash (generic), ported and verified for Java. Financial data validation has zero tolerance for false negatives — a missed invalid entry can corrupt downstream calculations. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

Java
// Crypto Transaction Hash (Generic)
// ReDoS-safe | RegexVault — Finance > Crypto

import java.util.regex.Pattern;

public class CryptoTransactionHashGenericValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^(?:0x)?[0-9a-fA-F]{64}$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef01234567890x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1b
abcdef0123456789abcdef0123456789abcdef0123456789abcdef01234567890xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789a
a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2notahash
0xGGGG

When to use this pattern

This pattern is drawn from the Finance > Crypto category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Transaction hashes are not globally unique across blockchains — the same hash string could theoretically appear on different chains (extremely unlikely but true). Include the chain identifier in your data model.

Technical Notes

Ethereum transactions use the 0x prefix. Bitcoin transactions do not. Both produce 256-bit (64 hex chars) hashes. Solana uses Base58-encoded 64-byte hashes — use a different pattern for Solana txids.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern