加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式提取标签的内容

发布时间:2020-12-14 06:02:56 所属栏目:百科 来源:网络整理
导读:这里有一点大脑冻结所以我希望有一些指针,基本上我需要提取特定div标签的内容,是的我知道正则表达式通常不被批准用于此但它是一个简单的网络抓取应用程序,其中没有嵌套的div. 我想要匹配这个: div class="entry" span class="title"Some company/span span
这里有一点大脑冻结所以我希望有一些指针,基本上我需要提取特定div标签的内容,是的我知道正则表达式通常不被批准用于此但它是一个简单的网络抓取应用程序,其中没有嵌套的div.

我想要匹配这个:

<div class="entry">
  <span class="title">Some company</span>
  <span class="description">
  <strong>Address: </strong>Some address
    <br /><strong>Telephone: </strong> 01908 12345
  </span>
</div>

简单的vb代码如下:

Dim myMatches As MatchCollection
    Dim myRegex As New Regex("<div.*?class=""entry"".*?>.*</div>",RegexOptions.Singleline)
    Dim wc As New WebClient
    Dim html As String = wc.DownloadString("http://somewebaddress.com")
    RichTextBox1.Text = html
    myMatches = myRegex.Matches(html)
    MsgBox(html)
    'Search for all the words in a string
    Dim successfulMatch As Match
    For Each successfulMatch In myMatches
        MsgBox(successfulMatch.Groups(1).ToString)
    Next

任何帮助将不胜感激.

解决方法

你的正则表达式适用于你的例子.但是,应该做出一些改进:

<div[^<>]*class="entry"[^<>]*>(?<content>.*?)</div>

[^<>] *表示“匹配除尖括号之外的任意数量的字符”,确保我们不会意外地突破我们所在的标记.

.*? (注意?)表示“匹配任意数量的字符,但只能尽可能少”.这避免了从第一个到最后一个< div class =“entry”>的匹配.在您的页面中标记.

但是你的正则表达式本身应该仍然匹配.也许你没有正确使用它?

我不知道Visual Basic,所以这只是在黑暗中拍摄,但RegexBuddy建议采用以下方法:

Dim RegexObj As New Regex("<div[^<>]*class=""entry""[^<>]*>(?<content>.*?)</div>")
Dim MatchResult As Match = RegexObj.Match(SubjectString)
While MatchResult.Success
    ResultList.Add(MatchResult.Groups("content").Value)
    MatchResult = MatchResult.NextMatch()
End While

我建议不要采取比这更进一步的正则表达式方法.如果你坚持,你最终会得到如下的怪物正则表达式,只有当div的内容形式永远不变时才会起作用:

<div[^<>]*class="entry"[^<>]*>s*
<span[^<>]*class="title"[^<>]*>s*
(?<title>.*?)
s*</span>s*
<span[^<>]*class="description"[^<>]*>s*
<strong>s*Address:s*</strong>s*
(?<address>.*?)
s*<strong>s*Telephone:s*</strong>s*
(?<phone>.*?)
s*</span>s*</div>

或者(看到VB.NET中多行字符串的乐趣):

Dim RegexObj As New Regex(
    "<div[^<>]*class=""entry""[^<>]*>s*" & chr(10) & _
    "<span[^<>]*class=""title""[^<>]*>s*" & chr(10) & _
    "(?<title>.*?)" & chr(10) & _
    "s*</span>s*" & chr(10) & _
    "<span[^<>]*class=""description""[^<>]*>s*" & chr(10) & _
    "<strong>s*Address:s*</strong>s*" & chr(10) & _
    "(?<address>.*?)" & chr(10) & _
    "s*<strong>s*Telephone:s*</strong>s*" & chr(10) & _
    "(?<phone>.*?)" & chr(10) & _
    "s*</span>s*</div>",RegexOptions.Singleline Or RegexOptions.IgnorePatternWhitespace)

(当然,现在你需要存储MatchResult.Groups(“title”)等的结果……)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读