5

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Shruts_me
  • 843
  • 2
  • 12
  • 24

2 Answers2

8

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

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Note that the second argument in `re.sub(...)` can be passed a function to. This function will be called for each substitution with a matches object. The returned string from this function will be the replacement for that substitution. This might be useful when processing variables in a string, or things alike. – Tim Visée Nov 02 '17 at 15:43
4

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'

Beamery
  • 171
  • 4