C# regular expression for finding forms with input tags in HTML?

Posted by johnrl on Stack Overflow See other posts from Stack Overflow or by johnrl
Published on 2010-05-05T13:55:46Z Indexed on 2010/05/05 13:58 UTC
Read the original article Hit count: 224

Filed under:
|
|
|

Hi all. I have a simple problem: I want to construct a regex that matches a form in HTML, but only if the form has any input tags. Example:

The following should be matched (ignoring attributes):

..
<form>
..
<input/>
..
</form>
..

But the following should not (ignoring attributes):

..
<form>
..
</form>
..

I have tried everything from look-arounds to capture groups but it quickly gets complicated. I want to believe there is a simple regex to capture the problem. Please note that it is important that the regex pairs the opening and closing tags according to the HTML code which means the following does not work:

<form>.+<input/>.+</form>

because it matches wrongly like this:

..
<form> <--- This is wrongly matched as the opening tag 
..
</form> 
<form> <-- This is the correct opening tag of the correct form
..
<input/>
..
</form> <--- This is matched as the closing tag
..

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex