I am trying to write a regex which match the first ending form tag.
[^~]*
The above regex
Just make the pattern non-greedy so that it matches the smallest possible amount of characters instead of the largest possible:
<form[^>]*name="loginForm"[^>]*>[^~]*?</form>
Edit:
Changed .*
to [^>]*
in the form tag, so that it doesn't match outside the tag.
Use a real parser like DOMDocument, SimpleXML or SimpleHTMLDOM. Regular expressions are not suitable for parsing non-regular languages like HTML.
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.