Python (re)

Python f-String Expression in PY

Match `{expression}` placeholders inside Python f-strings (or any single-brace template syntax).

Try it in the PY tester →

Pattern

regexPY
\{([^{}]+)\}   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\{([^{}]+)\}")
input_text = "f\"Hello {name}, you are {age} years old\""
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

\{ matches the literal opening brace. ([^{}]+) captures one or more characters that are NOT another brace — this avoids accidentally matching `{{` (the literal-brace escape in f-strings) and prevents nested-brace headaches. \} matches the closing brace.

Examples

Input

f"Hello {name}, you are {age} years old"

Matches

  • {name}
  • {age}

Input

f"Total: {amount:.2f} USD"

Matches

  • {amount:.2f}

Input

f"escaped {{ not a placeholder }}"

No match

Same pattern, other engines

← Back to Python f-String Expression overview (all engines)