Python regex: Fix one html close tag

<div>random contents without < or > , but has ( )  <div>

Just need to fix the closing div tag

so it looks like <div>random contents</div>

I need to do it in Python by regex.

The input is exact like the first line, there will no any < or > in random contents

Answers


replace

(<div>[^<]*<)(div>)

with

$1/$2

Note: This is bad practice, don't do it unless it's absolutely necessary!


I wouldn't recommend a regex - use something like tidy (which is a Python wrapper around HTML Tidy).


Avoid using regular expressions for dealing with HTML.

This is how it would be parsed in a DOM tree as it currently is:

>>> from BeautifulSoup import BeautifulSoup
>>> BeautifulSoup('<div>random contents<div>')
<div>random contents<div></div></div>

Or are you wanting to turn the second <div> into </div> (which a browser certainly would not do)?


Need Your Help

Pass data in a .getJSON. Possible? How?

c# jquery mvvm knockout.js

I'm doing an app and I got a select where the user can choose different Teams. Each team contains a couple of patients. I save the chosen team with the data-bind selectedOptions and stores the opti...

Moving the axes in d3.js

javascript d3.js svg

I have two axes in my graph right now, that are stuck at the very left and bottom of the graph. I want to make the axes line up with the (0,0) coordinate, or in other words I want the axes to be at...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.