我有这个网址
我正在尝试从URL中提取31.888433,73.263572 并将31.888433发送到TextBox 1 和73.263572到TextBox 2
你能给我一个例子,我怎么能用正则表达式或其他任何东西来做这件事
答案 0 :(得分:1)
您可以使用string.split()。该方法采用一系列字符,这些字符是分割的判别式。更好的解决方案是用'/'拆分,取以'@'开头的字符串然后用','拆分。你将拥有一个包含两个字符串的数组:第一个纬度,第二个经度。 应立即使用LINQ
答案 1 :(得分:1)
解释在代码注释中。
Dim strURL As String = "https://www.google.com/maps/place/Aleem+Iqbal+SEO/@31.888433,73.263572,17z/data=!3m1!4b1!4m5!3m4!1s0x39221cb7e4154211:0x9cf2bb941cace556!8m2!3d31.888433!4d73.2657607"
'Find the index of the first occurance of the @ character
Dim index As Integer = strURL.IndexOf("@")
'Get the string from that the next character to the end of the string
Dim firstSubstring As String = strURL.Substring(index + 1)
'Get a Char array of the separators
Dim separators As Char() = {CChar(",")}
'Split the string into an array based on the separator; the separator is not part of the array
Dim strArray As String() = firstSubstring.Split(separators)
'The first and second elements of the array is what you want
Dim strTextBox1 As String = strArray(0)
Dim strTextBox2 As String = strArray(1)
Debug.Print($"{strTextBox1} For TextBox1 and {strTextBox2} for TextBox2")
答案 2 :(得分:0)
最后制作了一个有效的代码我自己
Private _reg As Regex = New 正则表达式(“@( - ?[\ d] 。[\ d] ),( - ?[\ d] 。[\ d] )”,RegexOptions.IgnoreCase ) Private Sub FlatButton1_Click(sender As Object,e As EventArgs)处理FlatButton1.Click Dim url As String = WebBrowser2.Url.AbsoluteUri.ToString()
' The input string.
Dim value As String = WebBrowser2.Url.ToString
Dim myString As String = WebBrowser2.Url.ToString
Dim regex1 = New Regex("@(-?\d+\.\d+)")
Dim regex2 = New Regex(",(-?\d+\.\d+)")
Dim match = regex1.Match(myString)
Dim match2 = regex2.Match(myString)
If match.Success Then
Dim match3 As String = match.Value.Replace("@", "")
Dim match4 As String = match2.Value.Replace(",", "")
Label1.Text = match3
Label2.Text = match4
End If
End Sub
答案 3 :(得分:0)
Dim url As String = "www.google.com/maps/place/Aleem+Iqbal+SEO/@31.888433,73.263572,17z/data=!3m1!4b1!4m5!3m4!1s0x39221cb7e4154211:0x9cf2bb941cace556!8m2!3d31.888433!4d73.2657607"
Dim temp As String = Regex.Match(url, "@.*,").Value.Replace("@", "")
Dim arrTemp As String() = temp.Split(New String() {","}, StringSplitOptions.None)
Label1.Text = arrTemp(0)
Label2.Text = arrTemp(1)