Git Commit SHA in PY
Match Git commit hashes, both short (7 chars) and full (40 chars) forms.
Try it in the PY tester →Pattern
regexPY
\b[0-9a-f]{7,40}\b (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"\b[0-9a-f]{7,40}\b")
input_text = "commit 75d2cb0"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
Hexadecimal string of 7 to 40 lowercase characters, word-bounded. Covers Git's abbreviated SHAs and full SHA-1 hashes.
Examples
Input
commit 75d2cb0Matches
75d2cb0
Input
e7827cc1234567890abcdef1234567890abcdef1Matches
e7827cc1234567890abcdef1234567890abcdef1
Input
zzzzzzzNo match
—