Vb.net 需要使用正则表达式获取链接ID的帮助吗?

Vb.net 需要使用正则表达式获取链接ID的帮助吗?,vb.net,url,expression,Vb.net,Url,Expression,例如,我得到了这个url”http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402" 我想得到最后一位数字,其余数字可以是任何数字 我需要这样的格式“http://www.yellowpages.com/anything.... lid=randomdigitnumbers“或者只要我得到这些数字 我在正则表达式方面的知识非常贫乏,所以请大家帮帮我 以下方法不起

例如,我得到了这个url”http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"

我想得到最后一位数字,其余数字可以是任何数字

我需要这样的格式“http://www.yellowpages.com/anything.... lid=randomdigitnumbers“或者只要我得到这些数字

我在正则表达式方面的知识非常贫乏,所以请大家帮帮我

以下方法不起作用

Dim r As New System.Text.RegularExpressions.Regex("http://www.yellowpages.com/.*lid=d*", RegexOptions.IgnoreCase)
    Dim m As Match = r.Match(txt)
    If (m.Success) Then
        Dim int1 = m.Groups(1)
        MsgBox("(" + int1.ToString() + ")" + "")
    End If

提前感谢您使用正则表达式,因为我认为这有点过分了

您可以使用字符串函数完成相同的任务:

Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"

Dim queryString As String = url.SubString(url.IndexOf("?"), url.Length - url.IndexOF("?"))

Dim nameValuePairs As String() = queryString.Split("=")

Dim lid As String = nameValuePairs(1)
这是我的头顶,所以你可能需要调整一下。基本概念是将URL的一部分放在?(查询字符串),然后在=号上拆分它,取结果数组的第二个元素(值)


此外,如果查询字符串有多个名称-值对,它们将由
&
分隔,因此您需要首先在符号(
&
)上拆分,然后是等号。

只需找到
lid=
,然后获取所有内容:

Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
Dim lidIndex As Integer = url.IndexOf("lid=") + "lid=".Length
Dim lid As Integer = url.Substring(lidIndex)

简单地使用字符串函数会更容易做到这一点。+1用于比我的解决方案更短的解决方案-尽管如果查询字符串中有多个参数,我的解决方案也会遇到同样的问题。谢谢,我想我也做了同样的事情。虽然我真的很想用正则表达式得到它,但我想我会在其他时间得到它哦,对了!刚才也做了同样的事情。但是谢谢