5
votes

I have a line which has lots of words and characters. I just want to remove the part which is included in double curly braces

{{ }}

I tried ?={{.*}} but I am not getting anything.

2
Can you have nested curly braces? - Mark Byers
That's because Python doesn't deal with curly braces. See from __future__ import braces (I'm kidding, for anyone new to Python) - SomeKittens
So it is nested but between two consecutive braces are continuous and nothing in between them - Shruts_me

2 Answers

7
votes

Try this:

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

Note that { and } 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 it working online: ideone

4
votes

If you are trying to extract the text from inside the curly braces, try something like:

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

group(1) will contain the string 'pear'