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
  • 561,645
  • 34
  • 376
  • 476
Shruts_me
  • 833
  • 2
  • 11
  • 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
  • 767,688
  • 176
  • 1,542
  • 1,434
  • 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