[ACCEPTED]-Regex to match the first ending HTMl tag-regex
Accepted answer
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.
Use a real parser like DOMDocument, SimpleXML or SimpleHTMLDOM. Regular expressions 2 are not suitable for parsing non-regular 1 languages like HTML.
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.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.