我想解析html内容并将内容从A保留到B. 例如:
some content1...
<!-- begin_here -->
some content2
<!-- end_here -->
some content3
将成为
<!-- begin_here -->
some content2
<!-- end_here -->
现在,我使用sed来做:
sed '/begin_here/,/end_here/!d' file.html > file2.html
但是,我想使用python重写它以实现跨平台目的。 我对python中的正则表达式不是很熟悉。 可以给我一些提示吗? 非常感谢:)
答案 0 :(得分:2)
你可以在没有正则表达式的情况下这样做:
add_next = False # Do not add lines
# Until you encounter the first "start_here", which sets it to True
with open("file1.html", "r") as in_file:
with open("file2.html", "w") as out_file:
for line in in_file:
if "end_here" in line: # or line.startswith("end_here") for example
add_next = False
if add_next:
out_file.write(line)
if "begin_here" in line:
add_next = True
答案 1 :(得分:2)
使用多行正则表达式
import re
pat = re.compile('''^<!-- begin_here -->.*?<!-- end_here -->$''',
re.DOTALL + re.MULTILINE)
with open("file.txt") as f:
print pat.findall(f.read())