如何从字符串中删除两个标记之间出现的所有子字符串?

时间:2017-11-25 16:13:15

标签: python regex

我尝试删除以下字符串中<pre><code></code></pre>之间出现的所有子字符串,并删除<pre><code></code></pre>

txt = '<p>Large pythons were <pre><code> the subject of many </code></pre> a news story </p>\n last year due to the fact that there were at least two deaths <pre><code> directly attributable to them </code></pre>. Tragically, the deaths would not have happened had <pre><code> the owners of these snakes kept them </code></pre> safely, and responsibly, contained. The following article, by David Chiszar, Hobart M. Smith, <a href= Albert Petkus and Joseph Dougherty </a>, was recently published in the Bulletin of the Chicago Herpetological Society, and represents the first clear, and accurate, <p> account of the death that occurred July 1993</p>\n'

我编写了以下代码,以便在三次出现的子串中删除这些标记。

def remsubstr( s, first, last ):
if first and last not in s:
    return s

try:
    start = s.index( first ) + len( first )
    end = s.index( last, start )
    d = (s[:start] +" "+ s[end:]).replace('<p>', '').replace('</p>\n', '')
    started = d.index("<pre><code>" )
    ended = d.index("</code></pre>") + len("</code></pre>")
    nw = d.replace(d[started:ended], '')

    if first and last in nw:
        start = nw.index( first ) + len( first )
        end = nw.index( last, start )
        d1 = (nw[:start] +" "+ nw[end:])
        started = d1.index("<pre><code>" )
        ended = d1.index("</code></pre>") + len("</code></pre>")
        nw1 = d1.replace(d1[started:ended], '')

        if first and last in nw1:
            start = nw1.index( first ) + len( first )
            end = nw1.index( last, start )
            d2 = (nw1[:start] +" "+ nw1[end:])
            started = d2.index("<pre><code>" )
            ended = d2.index("</code></pre>") + len("</code></pre>")
            nw2 = d2.replace(d2[started:ended], '')
            return nw2

        return nw1

    return nw

except ValueError:
    return ""

我可以使用上面的代码删除所有必需的代码:

remsubstr(txt,"<pre><code>", "</code></pre>")

结果:

'Large pythons were  a news story  last year due to the fact that there were at least two deaths . Tragically, the deaths would not have happened had  safely, and responsibly, contained. The following article, by David Chiszar, Hobart M. Smith, <a href= Albert Petkus and Joseph Dougherty </a>, was recently published in the Bulletin of the Chicago Herpetological Society, and represents the first clear, and accurate,  account of the death that occurred July 1993'

我有数千个字符串,应该应用该函数来删除此场景的多次出现。

寻求帮助来编写代码,删除代码之间的所有子字符串,并且这些代码适用于子字符串/标记的三个以上实例。

2 个答案:

答案 0 :(得分:3)

我建议使用BeautifulSoup。在那里你可以组合.find_all()和.decompose()。在你的情况下,这应该这样做:

import bs4

txt = '<p>Large pythons were <pre><code> the subject of many </code></pre> a news story </p>\n last year due to the fact that there were at least two deaths <pre><code> directly attributable to them </code></pre>. Tragically, the deaths would not have happened had <pre><code> the owners of these snakes kept them </code></pre> safely, and responsibly, contained. The following article, by David Chiszar, Hobart M. Smith, <a href= Albert Petkus and Joseph Dougherty </a>, was recently published in the Bulletin of the Chicago Herpetological Society, and represents the first clear, and accurate, <p> account of the death that occurred July 1993</p>\n'
soup = bs4.BeautifulSoup(txt, "html.parser")
for tag in soup.find_all('pre'):
    if tag.find('code'):
        tag.decompose()

result = str(soup)

答案 1 :(得分:-1)

使用Beautiful Soup 4,标准字符串操作不适合XML文件中的嵌套。