正则表达式|删除两个单词中多行的单词

时间:2016-11-08 16:11:50

标签: python regex python-3.x

应删除的时间间隔在文章开始前的标记<p>Advertisement</p>和最终标记</time>内。如您所见,正则表达式应删除多行中的单词。 在一篇类似的帖子中,我被建议使用这个正则表达式。 但是,那个删除了我想要保存的第一个标记和属性。

import re

text='''
<p>Advertisement</p>, <p class="byline-dateline"><span class="byline"itemprop="author creator" itemscope="" itemtype="http://schema.org/Person">By <span class="byline-author" 
data-byline-name="MILAN SCHREUER" itemprop="name">MILAN SCHREUER</span> and </span><span class="byline" 
itemid="http://topics.nytimes.com/top/reference/timestopics/people/r/alissa_johannsen_rubin/index.html" 
itemprop="author creator" itemscope="" itemtype="http://schema.org/Person"><a href="http://topics.nytimes.com/top/reference/timestopics/people/r/alissa_johannsen_rubin/index.html" 
title="More Articles by ALISSA J. RUBIN"><span class="byline-author" data-byline-name="ALISSA J. RUBIN" data-twitter-handle="Alissanyt" itemprop="name">ALISSA J. RUBIN</span></a></span><time class="dateline" content="2016-10-06T01:02:19-04:00" 
datetime="2016-10-06T01:02:19-04:00" itemprop="dateModified">OCT. 5, 2016</time>
</p>, <p class="story-body-text story-content" data-para-count="163"   data-total-count="163">BRUSSELS — A man wounded two police officers with a knife in Brussels around noon on Wednesday in what the authorities called “a potential terrorist attack.”</p>, <p class="story-body-text story-content" 
data-para-count="231" data-total-count="394">The two officers were attacked on the Boulevard Lambermont in the Schaerbeek district, just north of the city center. A third police officer, who came to their aid, was also    injured. None of the three had life-threatening injuries.</p>
'''
my_pattern=("[\s\S]+\<\/time\>[\s\S]\<\/p\>\,\s")  
results= re.sub(my_pattern," ", text)
print(results)

1 个答案:

答案 0 :(得分:0)

my_pattern=("(?<=<p>Advertisement</p>)[\s\S]+\<\/time\>[\s\S]\<\/p\>\,\s")