【问题标题】:Filter out Html Number Via Regex通过正则表达式过滤出 Html 编号
【发布时间】:2016-03-18 22:04:45
【问题描述】:

我试图从 html 源中过滤掉具有特定数字的特定行

问题在于它在10025000 inc 之间的+50

我怎样才能使它在10025000 之间进行检查而不编写 100 行代码? txtAmount.text contains the website html source

这是我目前得到的:

   Const Amount = "(<td class=""text-right"">100</td>)|(<td class=""text-right"">150</td>)|(<td class=""text-right"">200</td>)|(<td class=""text-right"">etc</td>)"

     Dim qty As New List(Of String)
            qty = txtAmount.Lines.ToList
            For i As Integer = qty.Count - 1 To 0 Step -1
                If Not Regex.IsMatch(qty(i), Amount) Then
                    qty.RemoveAt(i)
                End If
            Next
            txtAmount.Lines = qty.ToArray



HTML:
    <td class="text-right">100</td> <=== I need to get this number

【问题讨论】:

  • 偶然的类名是什么?你的例子是空的,没有名字......
  • 抱歉,这里是 HTML:&lt;td class="text-right"&gt;Quantity&lt;/td&gt; &lt;td class="text-right"&gt;100&lt;/td&gt;
  • 你只是想从字符串中获取数值?

标签: html .net regex vb.net parsing


【解决方案1】:

您可以使用\d 元字符仅捕获数字(在此示例中,从 3 位到 5 位):

Dim html As String = 
    "<td class=""text-right"">Quantity</td> <td class=""text-right"">100</td>"

Dim rgx As New Regex(".+text-right.+>(?<value>\d{3,5})<.+", RegexOptions.Singleline)

If rgx.IsMatch(html) Then
    Dim value As Integer = CInt(rgx.Match(html).Groups("value").Value)
    Console.WriteLine(value) ' 100 (or whatever other digits exists in the html field.)
End If

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多