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.
Use re. sub() to remove text within parentheses sub(pattern, replacement, string) with pattern as the regular expression r"\([^()]*\)" and replacement as "" to remove text within parentheses in string .
To remove square brackets from the beginning and end of a string using Python, we pass “[]” to the strip() function as shown below. If you have curly brackets as well, we pass “[]{}” to strip() to remove the brackets.
To match literal curly braces, you have to escape them with \ . However, Apex Code uses \ as an escape, too, so you have to "escape the escape". You'll need to do this almost every time you want to use any sort of special characters in your regexp literally, which will happen more frequently than not.
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
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With