[ACCEPTED]-Regex to match the first ending HTMl tag-regex

Accepted answer
Score: 11

Just make the pattern non-greedy so that 4 it matches the smallest possible amount 3 of characters instead of the largest possible:

<form[^>]*name="loginForm"[^>]*>[^~]*?</form>

Edit:
Changed 2 .* to [^>]* in the form tag, so that it doesn't 1 match outside the tag.

Score: 2

Use a real parser like DOMDocument, SimpleXML or SimpleHTMLDOM. Regular expressions 2 are not suitable for parsing non-regular 1 languages like HTML.

Score: 2

You should NOT use regular expressions, but 2 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 1 use minidom and ElementTree for Python.

More Related questions