HomeRegex LibraryGit Commit Hash (SHA-1 / SHA-256) Regex
100% Client-Side β€’ 0 B Data Leaves Browser
validation

Git Commit Hash (SHA-1 / SHA-256) Regex

Matches short (7-char) and full (40-char SHA-1) Git commit hashes.

Regular Expression Pattern:
/^[0-9a-fA-F]{7,40}$/
Valid Match Example:
7075d8a
Invalid / Non-Match Example:
git-commit-xyz

Regex Syntax Breakdown & Explanation

  • β€’^[0-9a-fA-F]{7,40}$ : Hexadecimal string between 7 and 40 characters in length.

Implementation in Popular Languages

JavaScript / TypeScript
const regex = /^[0-9a-fA-F]{7,40}$/;
const isValid = regex.test("7075d8a");
console.log(isValid); // true
Python (re)
import re

pattern = r"^[0-9a-fA-F]{7,40}$"
match = re.match(pattern, "7075d8a")
print(bool(match)) # True
Go (regexp)
package main
import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile(`^[0-9a-fA-F]{7,40}$`)
  fmt.Println(re.MatchString("7075d8a"))
}
Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

The regular expression pattern is /^[0-9a-fA-F]{7,40}$/. Matches short (7-char) and full (40-char SHA-1) Git commit hashes.