如果在您的例子给出的记录为一致的,你不需要使用正则表达式。把文本文件简单分割成令牌列表就可以了。例如:
txt = '\n10: Vikelis M, Rapoport AM. Role of antiepileptic drugs as preventive agents for \nmigraine. CNS Drugs. 2010 Jan 1;24(1):21-33. doi:\n10.2165/11310970-000000000-00000. Review. PubMed PMID: 20030417.\n\n\n21: Johannessen Landmark C, Larsson PG, Rytter E, Johannessen SI. Antiepileptic\ndrugs in epilepsy and other disorders--a population-based study of prescriptions.\nEpilepsy Res. 2009 Nov;87(1):31-9. Epub 2009 Aug 13. PubMed PMID: 19679449.\n\n\n'
lines = [token.replace('\n', '') for token in txt.split('.')]
for line in lines:
print line
将线打印线的参考文献中的每个元素:
10: Vikelis M, Rapoport AM
Role of antiepileptic drugs as preventive agents for migraine
CNS Drugs
2010 Jan 1;24(1):21-33
doi:10
2165/11310970-000000000-00000
Review
PubMed PMID: 20030417
21: Johannessen Landmark C, Larsson PG, Rytter E, Johannessen SI
Antiepilepticdrugs in epilepsy and other disorders--a population-based study of prescriptions
Epilepsy Res
2009 Nov;87(1):31-9
Epub 2009 Aug 13
PubMed PMID: 19679449
同样,如果你能相信一个记录的第一行的作者;第二个是标题,第三个是期刊等,你可以很快做到这一点。如果信息有点“上下文”,那么你可以在这时开始使用正则表达式。
好运。
已应用和测试:'re.findall(r'(\ d +):(。+?)PubMed PMID:(\ d {8})',data,re.M | re.S)' – tux21b 2010-07-23 16:35:15
什么是贪婪的匹配?这意味着正则表达式引擎可以匹配尽可能多的字符。这是默认行为。 OP发生的事情是,他的正则表达式匹配“\ d +:”的第一次出现,然后匹配最后发生的“\ d {8}。”,从而有效地匹配整个输入文本。 – 2010-07-23 16:50:15
感谢您的帮助,“非贪婪匹配”改变了很多事情:) RegEx确实是一个强大的工具! – 2010-07-23 16:50:32