【问题标题】:How extract multi words in string by vb.net如何通过vb.net提取字符串中的多个单词
【发布时间】:2021-01-22 08:40:49
【问题描述】:
<tr class="sh"onclick="ii.ShowShareHolder('7358,IRO1GNBO0008')">
    <td>ghanisha sherkat-</td>
    <td><div class='ltr' title="141,933,691">142 M</div></td>
    <td>52.560</td>
    <td>0</td>
    <td><div class=""/></td>
</tr>

我们希望在上述文本项下输出:

ghanisha sherkat
141,933,691
52.560
0

我的尝试:

Dim input as string="above text"

Dim c2 As String() = input.Split(New String() {"</td>"},StringSplitOptions.None)

Dim r As Integer

For r = 0 To c2.Length - 2
    MessageBox.Show(c2(r))
Next

其他我的尝试

   Dim sDelimStart As String = "<td>" 
                    Dim sDelimEnd As String = "</td>" 
                    Dim nIndexStart As Integer = input.IndexOf(sDelimStart) 
                    Dim nIndexEnd As Integer = input.IndexOf(sDelimEnd) 
                    
                         Res = Strings.Mid(input, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length)

                        MessageBox.Show(res) 
                    

通过这种方式提取“ghanisha sherkat”

如何提取其他物品?

现在怎么继续呢?谢谢

【问题讨论】:

  • 没有人帮助我吗?
  • 在解析标记语言时应避免在文本内搜索或使用正则表达式。相反,如果您有可用的解析器,请使用它来解析标记并提取元素和属性。我在回答中提供了两种方法;一种使用XmlDocument 和一些XPath,另一种使用您不应该使用的正则表达式。

标签: vb.net html-parsing


【解决方案1】:

直接搜索字符串或使用正则表达式解析标记代码(如 HTML)并不好。您可以改用 XmlDocument 并使用 &lt;?xml...?&gt; 标记将 HTML 解析为 XML。

1。使用XmlDocument

Dim input As String = <?xml version="1.0" encoding="utf-8"?>
<tr class="sh" onclick="ii.ShowShareHolder('7358,IRO1GNBO0008')">
    <td>ghanisha sherkat</td>
    <td><div class="ltr" title="141,933,691">142 M</div></td>
    <td>52.560</td>
    <td>0</td>
    <td><div class=""/></td>
</tr>.ToString()

Dim doc As New XmlDocument
doc.LoadXml(input)

' Fetch all TDs inside TRs using XPath
Dim tds = doc.SelectNodes("/tr/td")

For Each item As XmlNode In tds
    ' If element id a DIV
    If item.FirstChild.Name = "div" Then
        ' Get the title attribute
        Dim titleAttr = item.FirstChild.Attributes("title")
        If Not titleAttr Is Nothing Then
            Console.WriteLine(titleAttr.Value)
        End If
    End If
    Console.WriteLine(item.InnerText())
Next

您可以在此.NET Fiddle 上查看使用XmlDocument 的工作示例。

2。 Usign 正则表达式(不推荐

Dim input As String =
<tr class="sh" onclick="ii.ShowShareHolder('7358,IRO1GNBO0008')">
    <td>ghanisha sherkat</td>
    <td><div class="ltr" title="141,933,691">142 M</div></td>
    <td>52.560</td>
    <td>0</td>
    <td><div class=""/></td>
</tr>.ToString()

' Extract all TDs
Dim tds = Regex.Matches(input, "<td[^>]*>\s*(.*?)\s*<\/td>")

For Each td In tds
    Dim content = td.Groups(1).ToString()
    ' Check if element id DIV
    If Regex.IsMatch(content, "^<div") Then
        ' Check if DIV has a title attribute
        Dim title = Regex.Match(content, "<div.*title=""(.*?)"">(.*)<\/div>")
        If title.Length > 0 Then
            ' Print the first group for title 141,933,691
            Console.WriteLine(title.Groups(1))
            ' Print the second group for element content 142 M
            Console.WriteLine(title.Groups(2))
        End If
    Else
        Console.WriteLine(td.Groups(1))
    End If
Next

您可以在.NET Fiddle 看到一个使用正则表达式的工作示例。

结果

两个例子的结果是一样的:

ghanisha sherkat
141,933,691
142 M
52.560
0

【讨论】:

    【解决方案2】:

    正则表达式可以使这项工作变得更容易。

    我假设您要捕获的值是直接在 td 标记内或在 td 内的标题属性中的值。

    因此,您可以使用以下正则表达式来捕获所有以 td 标记开头的值,内部可能有或没有带有 title 属性的标记,然后在值之后有或没有右引号,并以关闭 td 标记结束.

        <td>(?:<.*title="")?([a-zA-Z0-9 \.,]*)""?.*\-?</td>
    

    这是 VB.Net 代码:

        Imports System.Text.RegularExpressions
    
        Public Class Form1
    
            Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                Dim input As String = "above text"
    
                Dim matches As MatchCollection = Regex.Matches(input, "<td>(?:<.*title="")?([a-zA-Z0-9 \.,]*)""?.*\-?</td>")
    
                ' Loop over matches.
                For Each m As Match In matches
                    MessageBox.Show(m.Groups(1).Value)
                Next
            End Sub
        End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2017-02-14
      • 2012-10-27
      • 2023-04-03
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多