Go (RE2)

GitHub Personal Access Token in GO

Match GitHub Personal Access Tokens (classic + fine-grained) and OAuth tokens by their `ghX_` prefix.

Try it in the GO tester →

Pattern

regexGO
gh[pousr]_[A-Za-z0-9]{36,255}   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`gh[pousr]_[A-Za-z0-9]{36,255}`)
	input := `Use ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt for the API`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

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 API

Matches

  • ghp_AbCd1234EfGh5678IjKl9012MnOp3456QrSt

Input

GITHUB_TOKEN=gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Matches

  • gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Input

no token here

No match

Same pattern, other engines

← Back to GitHub Personal Access Token overview (all engines)