I am trying to write a regex which match the first ending form tag.
[^~]*
The above regex
You should NOT use regular expressions, but parse it with DOM:
Javascript:
var forms = document.getElementsByTagName('form');
forms[0] // is the first form element.
PHP:
$dom = new DOMDocument();
$dom->loadHTML( $html );
$forms = $dom->getElementsByTagName('form');
$first = $forms->item(0); // reference to first form
You can use minidom and ElementTree for Python.