I am trying to write a regex which match the first ending form tag.
[^~]*
The above regex
Use a real parser like DOMDocument, SimpleXML or SimpleHTMLDOM. Regular expressions are not suitable for parsing non-regular languages like HTML.