Python (re)

Base64 String in PY

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

Try it in the PY tester →

Pattern

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

Python (re) code

pyPython
import re

pattern = re.compile(r"(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?")
input_text = "SGVsbG8gV29ybGQ="
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

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)