views:

293

answers:

3

Sample text: String -> content within the rev tag (via lxml).

I'm trying to remove the {{BLOCKS}} within the text.

I've used the following regex to remove simple, one line blocks:

p = re.compile('\{\{*.*\}\}')
nonBracketedString = p.sub('', bracketedString)

However this does not remove the first multi line bracketed section at the beginning of the content. How can one remove the multi-line, curly bracketed blocks?


EDIT:

Solution from answer:

p = re.compile('\{\{*?.*?\}\}', re.DOTALL)
nonBracketedString = p.sub('', bracketedString)
+1  A: 

Set the dotall flag-- this allows . to match newlines.

p = re.compile('\{\{*.*\}\}', re.DOTALL)
nonBracketedString = p.sub('', bracketedString)
sysrqb
When applied to the supplied string -> It seems to delete every thing except for the final [[]] block, for me.
Nazarius Kappertaal
+1  A: 

Set the dotall flag.

p = re.compile('\{\{*.*?\}\}', re.DOTALL)
nonBracketedString = p.sub('', bracketedString)

In the default mode, . matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline.

http://docs.python.org/library/re.html

Also, you'll need a non-greedy match between the brackets: .*?

Geert
Same result as sysrqb's. [[]] block is all that is left.
Nazarius Kappertaal
+1  A: 
>>> import urllib2
>>> import re
>>> s = "".join(urllib2.urlopen('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Italian%20War%20of%201542-1546&redirects&rvprop=content&format=xml').readlines())
>>> p = re.compile('\{\{.*?\}\}', re.DOTALL)
>>> re.sub(p, '', s)
'<?xml version="1.0"?><api><query><redirects><r from="Italian War of 1542-1546" to="Italian War of 1542\xe2\x80\x931546" /></redirects><pages><page pageid="3719774" ns="0" title="Italian War of 1542\xe2\x80\x931546"><revisions><rev xml:space="preserve">\n\n\n\nThe \'\'\'Italian War of 1542\xe2\x80\x9346\'\'\' was a conflict late in the [[Italian Wars]], ...

I've truncated the output here, but there's enough to see that it's working.

Robert Rossney