Go (RE2)

File Extension in GO

Captures the file extension from a filename.

Try it in the GO tester →

Pattern

regexGO
\.([A-Za-z0-9]+)$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\.([A-Za-z0-9]+)$`)
	input := `document.pdf`
	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

`\.` matches the literal dot. `([A-Za-z0-9]+)$` captures alphanumeric characters up to the end of the string.

Examples

Input

document.pdf

Matches

  • .pdf

Input

archive.tar.gz

Matches

  • .gz

Input

no-extension

No match

Same pattern, other engines

← Back to File Extension overview (all engines)