【发布时间】:2016-12-26 21:29:53
【问题描述】:
所以我试图在 python 中使用正则表达式从 BibTex 中获取字符串。这是我的字符串的一部分:
a = '''title = {The Origin ({S},
{Se}, and {Te})- {TiO$_2$} Photocatalysts},
year = {2010},
volume = {114},'''
我要抓取标题的字符串,即:
The Origin ({S},
{Se}, and {Te})- {TiO$_2$} Photocatalysts
我目前有这个代码:
pattern = re.compile('title\s*=\s*{(.*|\n?)},\s*\n', re.DOTALL|re.I)
pattern.findall(a)
但它只给了我:
['The Origin ({S},\n {Se}, and {Te})- {TiO$_2$} Photocatalysts},\n year = {2010']
如何在没有year 信息的情况下获取整个标题字符串?
很多时候,year 不在title 之后。所以我不能使用:
pattern = re.compile('title\s*=\s*{(.*|\n?)},\s*\n.*year', re.DOTALL|re.I)
pattern.findall(a)
【问题讨论】:
-
我试过了,但仍然无法解决我的问题。