首页 > 解决方案 > 如何在 VB.net 中使用正则表达式进行捕获

问题描述

我想dal.socks.ipvanish.com在 vb.net 中使用正则表达式从给定的源中捕获。

</td>
 <td class="StatTDLabel">Dallas</td>
 <td class="StatTDLabel">dal.socks.ipvanish.com</td>
</tr>

这是我的一段代码,但这不起作用我不知道为什么有人可以帮我弄清楚。

Dim str1 As Match = Regex.Match(TextBox1.Text, "<td class=""StatTDLabel"">(.*?)<\/td>\n                    <\/tr>")

TextBox2.Text = str1.Groups(1).Value

标签: regexvb.net

解决方案


请参阅此处的正则表达式说明: https ://regex101.com/r/pJAD31/1

基本上你必须匹配第二次出现,StatTDLabel">因为你的输入字符串有两次相同的出现。

在此处查看工作代码https://dotnetfiddle.net/W9lbVH

Imports System
Imports System.Text.RegularExpressions
                
Public Module Module1
    Public Sub Main()
        
        Dim pattern As String = "StatTDLabel"">.*?(StatTDLabel"">)(.*)<\/td>"
        Dim input As String = "</td> <td class=""StatTDLabel"">Dallas</td> <td class=""StatTDLabel"">dal.socks.ipvanish.com</td> </tr>"
        Dim options As RegexOptions = RegexOptions.Multiline
        Dim Match = Regex.Matches(input, pattern, options)
    
        Console.WriteLine (Match(0).Groups(2))
        
    End Sub
End Module

推荐阅读