Python f-String Expression
Match `{expression}` placeholders inside Python f-strings (or any single-brace template syntax), skipping `{{ }}` literal-brace escapes.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("(?<!\\{)\\{([^{}]+)\\}(?!\\})", "g");
const input = "f\"Hello {name}, you are {age} years old\"";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
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+.
Why it doesn't work in GO
Go's RE2 engine doesn't support lookarounds (`(?=...)`, `(?<=...)`, etc.) — they break the linear-time matching guarantee.
Approach
Restructure to capture the surrounding context as a group instead, or use JS / Python where lookarounds are available.
Read the full guide →Workaround code in Go (RE2)
package main
import (
"fmt"
"regexp"
"unicode"
)
// RE2 doesn't support lookarounds. The fix: match a BROADER candidate
// without the lookaround, then verify the condition in Go code.
//
// Example: instead of `(?=.*\d)[A-Za-z\d]{8,}` (require a digit),
// match the candidate then check `HasDigit(s)` separately.
func HasDigit(s string) bool {
for _, r := range s {
if unicode.IsDigit(r) {
return true
}
}
return false
}
func main() {
re := regexp.MustCompile(`[A-Za-z\d]{8,}`) // simplified, no lookaround
input := "Password1 weakpass StrongerOne9"
for _, candidate := range re.FindAllString(input, -1) {
if HasDigit(candidate) { // the lookaround condition, in code
fmt.Println(candidate)
}
}
}Match a broader candidate without the lookaround, then verify the constraint in Go. Keeps the linear-time guarantee.
Pattern
(?<!\{)\{([^{}]+)\}(?!\}) (flags: g)Raw source: (?<!\{)\{([^{}]+)\}(?!\})
How it works
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
—Input
f"{{literal}} but {value} is real"Matches
{value}
Common use cases
- •Static analysis of f-string usage
- •Linting for unsafe expressions in templates
- •Translating f-strings to .format() during downgrades
- •Generating docs from template files
Related patterns
Triple-Quoted String (Python / TS)
Text ProcessingMatch triple-quoted strings (Python docstrings, TypeScript triple-quote, etc.) including newlines.
JavaScript Template Literal Placeholder
Text ProcessingMatch `${expression}` placeholders inside JavaScript template literals.
Base64 String
Text ProcessingMatch Base64-encoded strings, including proper padding with = and == characters.
Python Import Statement
Text ProcessingMatch Python `import x` and `from x import y` statements, capturing the module and target.