1

I'm trying to find a form in HTML using Regex in Visual Basic .NET, though, there are different forms, and I want the one with certain attributes on it.

I want to find this one:

    <form method="post"

While there are others looking like this:

    <form method="get"

I already have the code for this, but my code cannot recognize where the first form ends and the next one starts, so I get this whole HTML script where the first form starts

    <form>

And the last form ends

    </form>

To understand better what I mean, check this out: http://rubular.com/r/HDU0yVFtIk

Jeff Reed
  • 231
  • 1
  • 6
  • 23
  • 1
    Just use an ungreedy pattern by adding `?` : `[^*]*?<\/form>`. Note that this won't detect *nested* tags. You may use a proper html parser, I read somewhere on SO "HTML Agility pack" or something like that. – HamZa Aug 18 '13 at 22:46
  • [greedy vs non-greedy](http://stackoverflow.com/questions/3075130/difference-between-and-for-regex/3075532#3075532) great explanation. – HamZa Aug 18 '13 at 22:49

2 Answers2

2

Are you trying to find the form with the post method?

If so, you're almost there.

<form method="post".*>[^*]*<\/form>

http://rubular.com/r/DAi75yjQqU

mechenbier
  • 3,037
  • 23
  • 31
  • Though if there are more than one
    – Jeff Reed Aug 18 '13 at 22:54
  • @user2230481 Have you looked into the [Regex](http://msdn.microsoft.com/en-us/library/System.Text.RegularExpressions.Regex.aspx) class in .Net? More specifically, the [Regex.Matches](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.matches.aspx) and [Regex.IsMatch](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.ismatch.aspx) methods. – mechenbier Aug 18 '13 at 23:05
1

You need to make your last * match non-greedy by appending a ? as in:

<form.*>[^*]*?<\/form>

per http://rubular.com/r/6cYBTxX85F

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106