用Python编写正则表达式来解析前面和后面的文本
我正在解析英文维基百科的XML转储文件,并且一直在用Python编写杀死正则表达式。我附上一个示例数据片段,如下所示:用Python编写正则表达式来解析前面和后面的文本,python,regex,Python,Regex,我正在解析英文维基百科的XML转储文件,并且一直在用Python编写杀死正则表达式。我附上一个示例数据片段,如下所示: LEXEME = [ ('ref', re.compile(r'([\s\S]{1,250})(<ref[^/>]*>[\s\S]*?</ref>)([\s\S]{1,250}))', re.M | re.I)), ('header', re.compile(r'(^|\n)((==[^=]+==)|(===[^=]+===)|
LEXEME = [
('ref', re.compile(r'([\s\S]{1,250})(<ref[^/>]*>[\s\S]*?</ref>)([\s\S]{1,250}))',
re.M | re.I)),
('header', re.compile(r'(^|\n)((==[^=]+==)|(===[^=]+===)|(====[^=]+====))\s*$',
re.M | re.I))]
艾米·吉恩·克洛布查尔({IPAc en|712; k|l|o|b|601;ӛr});生于5月25日,
1960年)是[[美国参议院的资历|资深][[美国
来自[明尼苏达州]的州参议员。她是委员会的成员
[[明尼苏达州民主农民劳工党]],该党的一个附属机构
[[民主党(美国)|民主党]]。她是最棒的
明尼苏达州第一位当选为参议员的女性,也是其中之一
在现任[[美国参议院]任职的21名妇女
她曾担任[[Hennepin]的[[县律师]]
明尼苏达州郡]],明尼苏达州人口最多的县。作为
律师,她曾与美国前副总统共事
国家|副总统]][[Walter Mondale].{{cite web|
作者=参议院网站|标题=明尼苏达州的美国参议员艾米
克洛布查尔:传记|年份=2007 |网址=
[URL]|访问日期=
2007-02-23档案馆=
[网址]
|archivedate=2007年2月21日}她被称为“正在崛起的人”
民主党中的“明星”
新闻| url=[url]| title=Huffington
贴上“美国最聪明的人”的名字。
参议员| last=Tsukayama | first=Hayley | date=3月15日,
2010年|工作日期|访问日期| 5月14日,
2017年|存档url=|存档日期=|死亡url=}}{{Cite
新闻| url=[url]| title=As
该州唯一的参议员克洛布查尔赢得了同情
注意|最后一次=迪齐克斯|第一次=辛西娅|日期=5月20日,
2009年|工作=MinnPost |访问日期=5月14日,
2017年|存档url=|存档日期=|死亡url=|语言=英语}
==早期生活和教育==
克洛布查尔出生于[明尼苏达州普利茅斯],是罗斯·凯瑟琳(née Heuberger)的女儿
70岁退休,二年级教师,{{Cite
新闻| url=[url]| title=Rose
克洛布查尔,参议员艾米·克洛布查尔的母亲,
死亡|最后一次=纳尔逊|第一次=蒂姆|进入日期=2017-02-22}和[[Jim]
克洛布查尔|詹姆斯·约翰“吉姆”克洛布查尔]],一位作家和退休作家
体育记者和专栏作家为“[[明星论坛]]”
新闻| url=[url]| title=Born
骑:吉姆·克洛布查尔和明尼苏达自行车的诞生
旅游|报纸=明星论坛报|访问日期=2017-02-22}艾米有一个
妹妹。吉姆的祖父母是斯洛文尼亚人
美国|斯洛文尼亚]]移民,他的父亲是一名矿工
[[铁范围]];艾米的外祖父母来自美国
[[瑞士]].{引述
web | url=[url]| title=1 | work=rootsweb.com | accessdate=11
2015年9月}
从这些数据中,我想解析1)ref标记和内容,以及2)节头。例如,ref标记及其内容表示
<ref name=bio>
{{cite web
|author=Senate Web site
|title=U.S.Senator for Minnesota Amy Klobuchar: Biography
|year=2007
|url=[URL]
|accessdate=2007-02-23
|archiveurl=[URL]
|archivedate = February 21, 2007}}
</ref>
我已经用以下代码成功地解析了这些字段:
import re
LEXEME = [
('ref', re.compile(r'<ref[^/>]*>[\s\S]*?</ref>)',
re.M | re.I)),
('header', re.compile(r'(^|\n)((==[^=]+==)|(===[^=]+===)|(====[^=]+====))\s*$',
re.M | re.I))]
GROUP_RE = re.compile(
'|'.join('(?P<{0}>{1})'.format(name, regex.pattern)
for name, regex in LEXEME),
re.M | re.I)
for match in GROUP_RE.finditer(content):
print(match.lastgroup, '\t', match.group(0), '\n')
# Output
ref <ref name=bio>{{cite web| author= Senate Web site| title = U.S. Senator for Minnesota Amy Klobuchar: Biography| year = 2007| url= [URL]| accessdate= 2007-02-23|archiveurl = [URL] |archivedate = February 21, 2007}}</ref>
ref <ref>{{Cite news|url=[URL]|title=Huffington Post names Klobuchar the smartest U.S. Senator|last=Tsukayama|first=Hayley|date=March 15, 2010|work=|access-date=May 14, 2017|archive-url=|archive-date=|dead-url=}}</ref>
ref <ref>{{Cite news|url=[URL]|title=As state's only senator, Klobuchar gains sympathetic attention|last=Dizikes|first=Cynthia|date=May 20, 2009|work=MinnPost|access-date=May 14, 2017|archive-url=|archive-date=|dead-url=|language=en}}</ref>
header ==Early life and education==
重新导入
词素=[
('ref',重新编译(r']*>[\s\s]*?),
re.M | re.I)(,
('header',重新编译(r'(^.\n)((=[^=]+==)|(==[^=]+==)|(===[^=]+==)|(====[^=]+==)\s*$,
re.M | re.I))]
组\u RE=RE.compile(
“|”join(“(?P{1})”格式(名称,regex.pattern)
名称、词素中的正则表达式),
re.M | re.I)
对于组\u RE.finditer(内容)中的匹配:
打印(match.lastgroup,'\t',match.group(0),'\n')
#输出
参考{引用网站|作者=参议院网站|标题=美国明尼苏达州参议员艾米·克洛布查尔:传记|年份=2007 | url=[url]|访问日期=2007-02-23 |存档url=[url]|存档日期=2007年2月21日}
参考{{引用新闻}url=[url]| title=Huffington Post提名Klobuchar为美国最聪明的参议员{last=Tsukayama | first=Hayley | date=2010年3月15日| work=| access date=2017年5月14日|存档url=|存档日期=|死亡url=}
参考{{引用新闻}url=[url]| title=作为该州唯一的参议员,克洛布查尔获得了同情的关注{最后一位=迪齐克斯|第一位=辛西娅|日期=2009年5月20日|工作=明波斯特|访问日期=2017年5月14日|存档url=|存档日期=|死亡url=|语言=英语}
header==早期生活和教育==
我想扩展当前的正则表达式,这样在解析ref标记及其内容时,我还可以获得最多250个字符的前后文本。
例如,我想
She previously served as the [[county attorney]] for [[Hennepin County, Minnesota]], the most populous county in Minnesota. As an attorney, she worked with former [[Vice President of the United States|Vice President]] [[Walter Mondale]].
<ref name=bio>
{{cite web
|author=Senate Web site
|title=U.S. Senator for Minnesota Amy Klobuchar: Biography
|year=2007
|url=[URL]
|accessdate=2007-02-23
|archiveurl=[URL]
|archivedate=February 21, 2007}}
</ref>
She has been called a "rising star" in the Democratic Party.<ref>{{Cite news|url=[URL]|title=Huffington Post names Klobuchar the smartest U.S
她曾担任明尼苏达州人口最多的[[Hennepin county,Minnesota]]的[[县律师]]。作为律师,她曾与前[[美国副总统|副总统][[沃尔特·蒙代尔]]合作。
{{引用网站
|作者=参议院网站
|title=美国明尼苏达州参议员艾米·克洛布查尔:传记
|年份=2007年
|url=[url]
|访问日期=2007-02-23
|archiveurl=[URL]
|存档日期=2007年2月21日}
她在民主党被称为“新星”
而不是
<ref name=bio>
{{cite web
|author=Senate Web site
|title=U.S. Senator for Minnesota Amy Klobuchar: Biography
|year=2007
|url=[URL]
|accessdate=2007-02-23
|archiveurl=[URL]
|archivedate=February 21, 2007}}
</ref>
{{引用网站
|作者=参议院网站
|title=美国明尼苏达州参议员艾米·克洛布查尔:传记
|年份=2007年
|url=[url]
|访问日期=2007-02-23
|archiveurl=[URL]
|存档日期=2007年2月21日}
因此,我修改了我的代码如下:
LEXEME = [
('ref', re.compile(r'([\s\S]{1,250})(<ref[^/>]*>[\s\S]*?</ref>)([\s\S]{1,250}))',
re.M | re.I)),
('header', re.compile(r'(^|\n)((==[^=]+==)|(===[^=]+===)|(====[^=]+====))\s*$',
re.M | re.I))]
LEXEME=[
('ref',重新编译(r'([\s\s]{1250})(]*>[\s\s]*?)([\s\s]{1250})),
re.M | re.I)(,
('header',重新编译(r'(^.\n)((=[^=]+==)|(==[^=]+==)|(===[^=]+==)|(====[^=]+==)\s*$,
re.M | re.I))]
出现了一些问题
1) 当多个ref标记连续出现在句子末尾时:
<ref>{{Cite
news|url=[URL]|title=Huffington
Post names Klobuchar the smartest U.S.
Senator|last=Tsukayama|first=Hayley|date=March 15,
2010|work=|access-date=May 14,
2017|archive-url=|archive-date=|dead-url=}}</ref><ref>{{Cite
news|url=[URL]|title=As
state's only senator, Klobuchar gains sympathetic
attention|last=Dizikes|first=Cynthia|date=May 20,
2009|work=MinnPost|access-date=May 14,
2017|archive-url=|archive-date=|dead-url=|language=en}}</ref>
{{Cite
新闻| url=[url]| title=Huffington
贴上“美国最聪明的人”的名字。
参议员| last=Tsukayama | first=Hayley | date=3月15日,
2010年|工作日期|访问日期| 5月14日,
2017年|存档url=|存档日期=|死亡url=}}{{Cite
新闻| url=[url]| title=As
该州唯一的参议员克洛布查尔赢得了同情
注意|最后一次=迪齐克斯|第一次=辛西娅|日期=5月20日,
2009年|工作=MinnPost |访问日期=5月14日,
2017年|存档url=|存档日期=|死亡url=|语言=英语}
预期的结果是
ref up-to-250-chars<ref>content</ref>up-to-250-chars
ref up-to-250-chars<ref>content</ref>up-to-250-chars
ref-up-250-chars-contentup-to-250-chars
参考最多250个字符内容最多250个字符
但是,代码仅捕获后一个ref ta
ref s-date=May 14,
2017|archive-url=|archive-date=|dead-url=|language=en}}</ref>
==Early life and education== Born in [[Plymouth, Minnesota]], Klobuchar is the daughter of Rose Katherine (née Heuberger), who
retired at age 70 from teaching second grade,<ref>{{Cite
news|url=[URL]|title=Rose
Klobuchar, mother of Sen. Amy Klobuchar,
dies|last=Nelson|first=Tim|access-date=2017-02-22}}</ref> and [[Jim
Klobuchar|James John "Jim" Klobuchar]], an author and a retired
sportswriter and columnist for the ''[[Star Tribune]]''.<ref>{{Cite
news|url=[URL]
for match in GROUP_RE.finditer(content):
start= max(0, match.start()-250)
end= min(len(content), match.end()+250)
matched_text= content[start:end]