我有以下文件:
this is the first line
and this is the second line
now it is the third line
wow, the fourth line
but now it's the fifth line
etc...
etc...
etc...
从“现在是第三行”到“但现在是第五行”,我如何复制这三行(不知道这些行的行号)?在 perl 中,您将执行以下操作:
/^now it is/../^but now/
python中的等价物是什么?
我有(显然只抓住了 1 行):
regex = re.compile("now it is")
for line in content:
if regex.match(line):
print line
编辑:
reg = re.compile(r"now it is.*but now it.*", re.MULTILINE | re.DOTALL)
matches = reg.search(urllib2.urlopen(url).read())
for match in matches.group():
print match
这打印:
n
o
w
i
t
i
s
.
.
.
即它返回字符而不是完整的行