Shruts_me
Shruts_me

Reputation: 893

Python regex to remove substrings inside curly braces

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.

Upvotes: 5

Views: 9333

Answers (2)

Beamery
Beamery

Reputation: 171

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'

Upvotes: 4

Mark Byers
Mark Byers

Reputation: 838416

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

Upvotes: 8

Related Questions