Go (RE2)

Python f-String Expression in GO

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

Try it in the GO tester →

Pattern

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

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\{([^{}]+)\}`)
	input := `f"Hello {name}, you are {age} years old"`
	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

\{ 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)