Go (RE2)

Base64 String in GO

Match Base64-encoded strings, including proper padding with = and == characters.

Try it in the GO tester →

Pattern

regexGO
(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?`)
	input := `SGVsbG8gV29ybGQ=`
	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

The pattern matches groups of 4 Base64 characters ((?:[A-Za-z0-9+\/]{4})*) then allows the trailing partial group: 3 chars + one =, or 2 chars + ==. This enforces valid Base64 padding rules.

Examples

Input

SGVsbG8gV29ybGQ=

Matches

  • SGVsbG8gV29ybGQ=

Input

dGVzdA==

Matches

  • dGVzdA==

Input

not!base64!@

No match

Same pattern, other engines

← Back to Base64 String overview (all engines)