我有以下文件:如何複製多行?
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
.
.
.
即它返回字符,而不是全線
這看起來不錯,但我不太清楚如何使用它....像re.compile(r「^現在是。*但現在$」,re.MULTILINE)? – 2012-04-19 00:10:03
@ user522962:類似的東西,但你也應該添加另一個標誌(['re.DOTALL'](http://docs.python.org/library/re.html#re.DOTALL)),這將使' .'匹配_newline_字符,並且還應該在'$'之前添加'。*'以匹配最後一行的剩餘部分。你想看到完整的解決方案嗎?或者這對解決問題來說足夠了嗎? – Tadeck 2012-04-19 00:50:10
我已經給它一個鏡頭......看到我的編輯在我的問題....我做錯了什麼。 – 2012-04-19 02:03:47