Python (re)

Data URI in PY

Match base64-encoded data URIs, including the MIME type and the base64 payload.

Try it in the PY tester →

Pattern

regexPY
data:[\w\/+\-.]+(?:;[\w=\-]+)*;base64,[A-Za-z0-9+\/=]+   (flags: gi)

Python (re) code

pyPython
import re

pattern = re.compile(r"data:[\w\/+\-.]+(?:;[\w=\-]+)*;base64,[A-Za-z0-9+\/=]+", re.IGNORECASE)
input_text = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA"
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

Starts with data:, then a MIME type (word chars, slashes, plus, hyphen, dot), optional parameters, then ;base64, and the base64 payload (letters, digits, +, /, =).

Examples

Input

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA

Matches

  • data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA

Same pattern, other engines

← Back to Data URI overview (all engines)