Чтобы удалить все с {{
до '''Papori'''
:
import re
regex = re.compile(r"""{{.*?}}\s*('''Papori''')""", re.DOTALL)
print regex.sub(r"\1", rev_data)
Чтобы удалить все с первого "{{" до соответствующего "}}":
prefix, sep, rest = rev_data.partition("{{")
if sep: # found the first "{{"
rest = sep + rest # put it back
while rest.startswith("{{"):
# remove nested "{{expr}}" one by one until there is none
rest, n = re.subn(r"{{(?:[^{]|(?<!{){)*?}}", "", rest, 1)
if n == 0:
break # the first "{{" is unmatched; can't remove it
else: # deletion is successful
rev_data = prefix + rest
print(rev_data)
Чтобы удалить все из первого "{{" в соответствующее "}}" без регулярного выражения:
prefix, sep, rest = rev_data.partition("{{")
if sep: # found the first "{{"
depth = 1
prevc = None
for i, c in enumerate(rest):
if c == "{" and prevc == c: # found "{{"
depth += 1
prevc = None # match "{{{ " only once
elif c == "}" and prevc == c: # found "}}"
depth -= 1
if depth == 0: # found matching "}}"
rev_data = prefix + rest[i+1:] # after matching "}}"
break
prevc = None # match "}}} " only once
else:
prevc = c
print(rev_data)
Полный пример
#!/usr/bin/env python
import urllib, urllib2
import xml.etree.cElementTree as etree
# download & parse xml, find rev data
params = dict(action="query", prop="revisions", rvprop="content",
format="xml", titles="papori", rvsection=0)
request = urllib2.Request(
"http://en.wikipedia.org/w/api.php?" + urllib.urlencode(params),
headers={"User-Agent": "WikiDownloader/1.0",
"Referer": "http://stackoverflow.com/q/7937855"})
tree = etree.parse(urllib2.urlopen(request))
rev_data = tree.findtext('.//rev')
# remove everything from the first "{{" to matching "}}"
prefix, sep, rest = rev_data.partition("{{")
if sep: # found the first "{{"
depth = 1
prevc = None
for i, c in enumerate(rest):
if c == "{" and prevc == c: # found "{{"
depth += 1
prevc = None # match "{{{ " only once
elif c == "}" and prevc == c: # found "}}"
depth -= 1
if depth == 0: # found matching "}}"
rev_data = prefix + rest[i+1:] # after matching "}}"
break
prevc = None # match "}}} " only once
else:
prevc = c
print rev_data
выход
'''Papori''' ({{lang-as|'''পাপৰী'''}}) is an [[Assamese
language]] feature film directed by [[Jahnu Barua]]. The film
stars Gopi Desai, [[Biju Phukan]], Sushil Goswami, Chetana Das
and Dulal Roy. The film was released in 1986.<ref name="ab">{{cite
web|url=http://www.chaosmag.in/barua.html|title=Papori – 1986 –
Assamese film|publisher=Chaosmag|accessdate=4 February
2010}}</ref>