【发布时间】:2015-07-14 07:30:17
【问题描述】:
我正在尝试从 Thomson-Reuters Web of Science 中提取出版年 ISI 风格的数据。 “出版年”这一行看起来像这样(在一行的开头):
PY 2015
对于我正在编写的脚本,我定义了以下正则表达式函数:
import re
f = open('savedrecs.txt')
wosrecords = f.read()
def findyears():
result = re.findall(r'PY (\d\d\d\d)', wosrecords)
print result
findyears()
但是,这会产生误报结果,因为该模式可能出现在数据的其他地方。
所以,我只想匹配一行开头的模式。通常我会为此使用^,但r'^PY (\d\d\d\d)' 无法匹配我的结果。另一方面,使用\n 似乎可以满足我的要求,但这可能会给我带来更多麻烦。
【问题讨论】:
-
使用
re.MULTILINE改变^的语义:re.findall(r'^PY (\d\d\d\d)', wosrecords, re.MULTILINE)