我试图在bibtex文件中搜索标题,仅将标题作为输出。
例如,当我逐行阅读bibtex文本时,它将是列表中的字符串:
['\tPages = {3221--52},\n',
'\tTitle = {The slave trade and the origins of mistrust in Africa},\n',
'\tVolume = {101},\n',
'\tYear = {2011}}\n',
'\n',
'@article{perez2013regime,\n',
"\tAuthor = {P{\\'e}rez-Li{\\~n}{\\'a}n, An{\\'\\i}bal and Mainwaring, Scott},\n",
'\tDate-Added = {2019-03-06 15:28:11 -0800},\n',
'\tDate-Modified = {2019-03-06 15:28:11 -0800},\n',
'\tJournal = {Comparative Politics},\n',
'\tNumber = {4},\n',
'\tPages = {379--397},\n',
'\tPublisher = {City University of New York},\n',
'\tTitle = {Regime legacies and levels of democracy: evidence from Latin America},\n',
'\tVolume = {45},\n']
从这里,我只想获取标题:
The slave trade and the origins of mistrust in Africa
Regime legacies and levels of democracy: evidence from Latin America
如何使用regex
来做到这一点?
可能我想要类似的东西:
for line in lines:
pattern = re.compile( regex here )
result = pattern.search(line)
if (result):
print(result.group())
我按照以下答案进行操作,但不会显示结果:
string = '\tTitle = {Regime legacies and levels of democracy: evidence from Latin America},\n'
pattern = re.compile(r'^\\tTitle = \{(.*)\},\\n$')
result = pattern.match(string)
if (result):
print(result.group(1))
我没有结果,意味着没有比赛?
答案 0 :(得分:0)
尝试一下:
^\\tTitle = \{(.*)\},\\n$
使用匹配而不是搜索