Попробуйте это:
>>> import re
>>> a = 'add {new} sentence {with} this word'
>>> re.findall(r'\{(\w+)\}', a)
['new', 'with']
Другой подход с использованием Formatter
:
>>> from string import Formatter
>>> a = 'add {new} sentence {with} this word'
>>> [i[1] for i in Formatter().parse(a) if i[1]]
['new', 'with']
Другой подход с использованием split()
:
>>> import string
>>> a = 'add {new} sentence {with} this word'
>>> [x.strip(string.punctuation) for x in a.split() if x.startswith("{") and x.endswith("}")]
['new', 'with']
Вы можете даже использовать string.Template
:
>>> class MyTemplate(string.Template):
... pattern = r'\{(\w+)\}'
>>> a = 'add {new} sentence {with} this word'
>>> t = MyTemplate(a)
>>> t.pattern.findall(t.template)
['new', 'with']
Что вы уже пробовали? Что не работает? – soon
Возможно '{(. *?)}' !!!! – NINCOMPOOP