3

i'm trying to get some information of a webpage via regex on visual basic 2010

it's something like this:

<SPAN CLASS="clear"></SPAN>
<h2> blabla </h2>
<h2> blabla </h2>
<b> blabla </b>

etc etc

<SPAN CLASS="clear"></SPAN>

what i want is everything between the 2 also the h2 tags and every other html tag that exists.

is this possible?

i've already tried (.?) and . and \w* but it doesn't return anything...

1
  • 1
    I think you don't need to work with RegEx, but use a XMlReader in your code. Commented Feb 17, 2012 at 15:50

2 Answers 2

5

It's probably best to use an XML parser for that, but I'm assuming it's a one-off scrape or similar.

If I understand you correctly, this should get all the data between the tags:

Dim regex As New Text.RegularExpressions.Regex("<.*?>", RegexOptions.Singleline)
Dim result As String = regex.Replace(yourHtml, String.Empty)

You could use this to get just the H2 tags and data:

Dim regex As New Text.RegularExpressions.Regex("<\s*h2[^>]*>(.*?)<\s*/\s*h2>", RegexOptions.Singleline)
Dim results As New Text.StringBuilder
For Each m As Text.RegularExpressions.Match In regex.Matches(yourHtml)
    results.Append(m.Value)
Next
Sign up to request clarification or add additional context in comments.

Comments

0

alundy already have good answer, yet you can try this one too.

Dim findtext2 As String = "(?<=<h2>)(.*?)(?=</h2>)"
Dim myregex2 As String = TextBox1.Text 'Your HTML code
Dim doregex2 As MatchCollection = Regex.Matches(myregex2, findtext2)
Dim matches2 As String = ""
For Each match2 As Match In doregex2
    matches2 = matches2 + match2.ToString + Environment.NewLine
Next
MsgBox(matches2) 'Results

Don't forget Imports System.Text.RegularExpressions.

Above code is getting all information between 2 strings, in this case - <h2> and </h2>. You can use whatever you want (it doesn't need to be tag, not even html).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.