Python regex to remove substrings inside curly braces

I have a string with a lot of words and characters. I just want to remove the part that is included in double curly braces

{{ }}

I tried ?={{.*}}, but get nothing.

+5
source share
2 answers

Try the following:

import re
s = re.sub('{{.*?}}', '', s)

Note that {both }are usually special characters in regular expressions and should usually be escaped with a backslash to get their literal meaning. However, in this context they are interpreted as literals.

See how it works on the Internet: ideone

+7
source

, - :

import re 
s = 'apple {{pear}} orange {banana}'
matches = re.search(r'{{(.*)}}', s)
print matches.group(1)

group(1) 'pear'

+4

All Articles