GitHub Personal Access Token in PY
Match GitHub Personal Access Tokens (classic + fine-grained) and OAuth tokens by their `ghX_` prefix.
Try it in the PY tester →Pattern
regexPY
gh[pousr]_[A-Za-z0-9]{36,255} (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"gh[pousr]_[A-Za-z0-9]{36,255}")
input_text = "Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the API"
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
gh[pousr]_ matches the GitHub token-type prefix: ghp_ (PAT classic), gho_ (OAuth), ghu_ (user-to-server), ghs_ (server-to-server), ghr_ (refresh). [A-Za-z0-9]{36,255} matches the token body — GitHub fine-grained tokens are longer than the classic 36-char tokens, so we allow up to 255.
Examples
Input
Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the APIMatches
ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt
Input
GITHUB_TOKEN=gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxMatches
gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Input
no token hereNo match
—