Skip to content

Regex for Base64 — Pattern, Examples & Caveats

Pattern
^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$

This pattern validates standard Base64 encoding: groups of four characters from the Base64 alphabet, optionally ending with a two- or three-character group padded by = or == to a multiple of four. It matches the canonical padded form. Identifier patterns recognize structured tokens such as UUIDs, hashes, Base64 strings and version numbers. These formats have exact layouts and alphabets defined by specifications, so the regex encodes group lengths, separators and allowed characters. A match confirms the shape; semantic validity (a real version, a correct checksum) usually needs an additional library check. It needs no special flags. Apply it to the whole input as a format contract. It accepts the empty string; add a + quantifier or a length check if empty input should be rejected. Base64URL uses - and _ instead of + and / and often omits padding — use a different pattern for URL-safe variants. A valid Base64 shape does not guarantee the decoded bytes are meaningful; decode and inspect for real validation. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • (?:[A-Za-z0-9+/]{4})* — zero or more complete four-character groups
  • [A-Za-z0-9+/]{2}== — final two data chars plus two padding =
  • [A-Za-z0-9+/]{3}= — final three data chars plus one padding =
  • (?: ... )? — the padded tail is optional (input may be an exact multiple of 3 bytes)
  • +/ — the standard Base64 alphabet's two non-alphanumeric symbols

What it matches

SGVsbG8=SGVsbG9vYWJjZA==(empty string)

What it does not match

SGVsbG8SGVsbG8===SGVsbG8-====

When to use it

Reach for an identifier pattern when you must recognize a standardized token — a UUID, a hash, a Base64 blob or a version string — before decoding or looking it up. The pattern confirms the layout and alphabet defined by the specification, which catches typos and wrong formats early. Follow it with a library that decodes or verifies semantics, since a correctly shaped identifier may still be invalid, expired or refer to nothing in your system.

Caveats

  • It accepts the empty string; add a + quantifier or a length check if empty input should be rejected.
  • Base64URL uses - and _ instead of + and / and often omits padding — use a different pattern for URL-safe variants.
  • A valid Base64 shape does not guarantee the decoded bytes are meaningful; decode and inspect for real validation.

Code examples

JavaScript
const re = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/;
const input = "SGVsbG8=";
if (re.test(input)) {
  // valid: proceed
} else {
  // reject with a helpful message
}
Python
import re

rx = re.compile(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$")
value = "SGVsbG8="
if rx.fullmatch(value):
    pass  # valid
else:
    raise ValueError("invalid format")
Java
import java.util.regex.*;

Pattern p = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
Matcher m = p.matcher("SGVsbG8=");
boolean ok = m.matches();   // whole input must conform
PHP
<?php
$subject = "SGVsbG8=";
if (preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $subject, $m)) {
    // $m[0] is the full match
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

Why does Base64 use = padding?

Base64 encodes every 3 bytes into 4 characters. When the input length is not a multiple of 3, = pads the final group to 4 characters so decoders know how many bytes to emit.

What is Base64URL?

A URL- and filename-safe variant replacing + with - and / with _, usually without padding. JWTs use it; the standard Base64 pattern here will reject those characters.

Does a matching identifier guarantee it is real?

No — it confirms the textual shape only. A UUID-shaped string may not be a generated UUID, and a Base64-shaped string may decode to nonsense. Use a library to decode, verify checksums or confirm semantics.

What does the +json or +xml suffix mean?

It is the structured-syntax suffix convention (RFC 6839): application/geo+json is JSON with a defined structure. It tells clients the underlying serialization while naming the specific format.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →