Jay Nanavaty
Jay Nanavaty

Reputation: 1129

Regex to detect file hash string

I have files named like this:

//Should detect as filehash name

98-80-7D-E5-50-A1-73-59-7F-FC-1C-CB-10-0F-39-D7-C2-97-88-44.zip

//Should detect as non-hash name

model.zip

I want to detect in C# that the file name is hash value or not? How do I detect such string? So far I've used following but it does not work.

private static bool IsFileHash(string input)
    {
        if (String.IsNullOrEmpty(input))
        {
            return false;
        }

        return Regex.IsMatch(input, "^[0-9a-fA-F]{32}$", RegexOptions.Compiled);
    }

Upvotes: 1

Views: 359

Answers (1)

Aziz.G
Aziz.G

Reputation: 3721

this will match a hash name : (\w{2}\-)*(\w){2}

const text = "98-80-7D-E5-50-A1-73-59-7F-FC-1C-CB-10-0F-39-D7-C2-97-88-44"

const regex = /(\w{2}\-)*(\w){2}/g;

console.log(text.match(regex));

Upvotes: 1

Related Questions