2012-10-15 21 views
0

我想解析HTML内容,并从一个保留的内容为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中的正则表达式不是很熟悉。 可以给我一些提示来做到这一点? 非常感谢:)

+1

你并不真的需要使用正则表达式这一点。如果您的分隔符总是相同,则对该文件进行迭代也应该可以工作。 – Blender

+0

@Blender哦,是的,听起来可行, 我可以readline和检查保持或ignore.thanks – CSJ

回答

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 
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()) 
相关问题