Removing all div tags from HTML string

python, regex

Solution

Do not use regex for this problem. Use an html parser. Here is a solution in python with BeautifulSoup:

from BeautifulSoup import BeautifulSoup

with open('Path/to/file', 'r') as content_file:
    content = content_file.read()

soup = BeautifulSoup(content)
[div.extract() for div in soup.findAll('div')]

with open('Path/to/file.modified', 'w') as output_file:
    output_file.write(str(soup))

Problem

I am trying to strip all divs. Input: ``` <p>111</p> <div class="1334">bla</div> <p>333</p> <p>333</p> <div some unkown stuff>bla2</div> ``` Desired Output: ``` <p>111</p> <p>333</p> <p>333</p> ``` I tried this but it isn't working: ``` release_content = re.sub("/<div>.*<\/div>/s", "", release_content) ```

Original source