use RegEx to extract text between html tags

2019-08-31 23:15发布

I have to extract from a string in visual basic some text, like this:

<div id="div">
<h2 id="id-date">09.09.2010</h2> , here to extract the date 

<h3 id="nr">000</h3> , here a number </div>

I need to extract the date from the div and the number all this from within the div... Also and this will be in loop, meaning there are more div block needed to be parsed.! thank you! Adrian

5条回答
Rolldiameter
2楼-- · 2019-08-31 23:38

You should not be parsing HTML with regular expressions because HTML is not regular as stated by Daniel Vandersluis. You can use the HTML Agility Pack

查看更多
【Aperson】
3楼-- · 2019-08-31 23:48

Try this taken from this link -

private string StripHTML(string htmlString)
{
    //This pattern Matches everything found inside html tags;
    //(.|\n) - > Look for any character or a new line
    // *?  -> 0 or more occurences, and make a non-greedy search meaning
    //That the match will stop at the first available '>' it sees, and not at the last one
    //(if it stopped at the last one we could have overlooked 
    //nested HTML tags inside a bigger HTML tag..)
    // Thanks to Oisin and Hugh Brown for helping on this one...

    string pattern = @"<(.|\n)*?>";  

    return  Regex.Replace(htmlString,pattern,string.Empty);
}
查看更多
劳资没心,怎么记你
4楼-- · 2019-08-31 23:50

If your HTML tag have attributes, then here is my solution:

<TAG(.*?)>(.*?)</TAG>

Example (using C#):

var regex = new System.Text.RegularExpressions.Regex("<h1(.*?)>(.*?)</h1>");
var m = regex.Match("Hello <h1 style='color: red;'>World</h1> !!");
Console.Write(m.Groups[2].Value); // will print -> World
查看更多
女痞
5楼-- · 2019-09-01 00:03
该账号已被封号
6楼-- · 2019-09-01 00:04

Parsing HTML with regex is not ideal. Others have suggested the HTML Agility Pack. However, if you can guarantee that your input is well-defined and you always know what to expect then using a regex is possible.

If you can make that guarantee, read on. Otherwise you need to consider the other suggestions or define your input better. In fact, you should define your input better regardless because my answer makes a few assumptions. Some questions to consider:

  • Will the HTML be on one line or multiple lines, separated by newline characters?
  • Will the HTML always be in the form of <div>...<h2...>...</h2><h3...>...</h3></div>? Or can there be h1-h6 tags?
  • On top of the hN tags, will the date and number always be between the tags with id-date and nr values for the id attribute?

Depending on the answers to these questions the pattern can change. The following code assumes each HTML fragment follows the structure you shared, that it will have an h2 and h3 with date and number, respectively, and that each tag will be on a new line. If you feed it different input it will likely break till the pattern matches your input's structure.

Dim input As String = "<div id=""div"">" & Environment.Newline & _
               "<h2 id=""id-date"">09.09.2010</h2>" & Environment.Newline & _
               "<h3 id=""nr"">000</h3>" & Environment.Newline & _
               "</div>"

Dim pattern As String = "<div[^>]+>.*?" & _
                 "<h2\sid=""id-date"">(?<Date>\d{2}\.\d{2}\.\d{4})</h2>.*?" & _
                 "<h3\sid=""nr"">(?<Number>\d+)</h3>.*?</div>"

Dim m As Match = Regex.Match(input, pattern, RegexOptions.Singleline)

If m.Success Then
    Dim actualDate As DateTime = DateTime.Parse(m.Groups("Date").Value)
    Dim actualNumber As Integer = Int32.Parse(m.Groups("Number").Value)
    Console.WriteLine("Parsed Date: " & m.Groups("Date").Value)
    Console.WriteLine("Actual Date: " & actualDate)
    Console.WriteLine("Parsed Number: " & m.Groups("Number").Value)
    Console.WriteLine("Actual Number: " & actualNumber)
Else
    Console.WriteLine("No match!")
End If

The pattern can be on one line but I broke it up for clarity. RegexOptions.Singleline is used to allow the . metacharacter to handle \n for newlines.

You also said:

Also and this will be in loop, meaning there are more div block needed to be parsed.

Are you looping over separate strings? Or are you expecting multiple occurrences of the above HTML structure in a single string? If the former, the above code should be applied to each string. For the latter you'll want to use Regex.Matches and treat each Match result similarly to the above piece of code.


EDIT: here is some sample code to demonstrate parsing multiple occurrences.

Dim input As String = "<div id=""div"">" & Environment.Newline & _
               "<h2 id=""id-date"">09.09.2010</h2>" & Environment.Newline & _
               "<h3 id=""nr"">000</h3>" & Environment.Newline & _
               "</div>" & _
               "<div id=""div"">" & Environment.Newline & _
               "<h2 id=""id-date"">09.14.2010</h2>" & Environment.Newline & _
               "<h3 id=""nr"">123</h3>" & Environment.Newline & _
               "</div>"

Dim pattern As String = "<div[^>]+>.*?" & _
                 "<h2\sid=""id-date"">(?<Date>\d{2}\.\d{2}\.\d{4})</h2>.*?" & _
                 "<h3\sid=""nr"">(?<Number>\d+)</h3>.*?</div>"

For Each m As Match In Regex.Matches(input, pattern, RegexOptions.Singleline)
    Dim actualDate As DateTime = DateTime.Parse(m.Groups("Date").Value)
    Dim actualNumber As Integer = Int32.Parse(m.Groups("Number").Value)
    Console.WriteLine("Parsed Date: " & m.Groups("Date").Value)
    Console.WriteLine("Actual Date: " & actualDate)
    Console.WriteLine("Parsed Number: " & m.Groups("Number").Value)
    Console.WriteLine("Actual Number: " & actualNumber)
Next
查看更多
登录 后发表回答