次のファイルがあります。
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...
「今は3行目です」から「しかし今は5行目です」まで、これらの3行を(それらの行の行番号を知らずに)どのようにコピーしますか?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
.
.
.
つまり、行全体ではなく文字を返します