从文本框中查找并替换一系列文本

时间:2016-03-08 16:59:13

标签: vb.net

进入 Visual Basic 项目我正在尝试替换来自文本框的网址的一部分 。例如,我将此https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg网址加入TextBox1,我想将部分/s912-替换为/s1600-

我是通过做这样的事情做到的:Dim url = Replace(TextBox1.Text, "/s912-", "/s1600-")。但是,这个URL每次都不一样。例如,/s912-部分可以是/s800-。所以我的下一个是使用asterik:Dim url = Replace(TextBox1.Text, "/s*-", "/s1600-")。当然它不起作用!所以我需要有关语法或更好主意的帮助。

3 个答案:

答案 0 :(得分:2)

Regex.Replace可用于使用regular expression进行搜索/替换。

Dim input = "https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg"
Dim output = Regex.Replace(input, "/s\d+-", "/s1600-")

output是:

  

https://lh3.googleusercontent.com/blahblahblah/s1600-Ic42/blahblahblah.jpg

答案 1 :(得分:1)

Dim url As String = "https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg"
Dim pattern As String = "\/[sS][0-9]+-"
Dim newPart As String = "/s1600-"
Dim newUrl As String

Dim m as Match = regex.Match(url, pattern)
If m.Success Then
    Dim curr as String = url.Substring(m.Index, m.Length)
    newUrl = url.Replace(curr, newPart)
End If

' Test
Console.WriteLine(newUrl)

结果:

  

https://lh3.googleusercontent.com/blahblahblah/s1600-Ic42/blahblahblah.jpg

答案 2 :(得分:1)

考虑这个实验。我想向您展示另一种方法,以及为什么Regex有时可能是有害的。虽然,我不是说在你的情况下你不应该使用正则表达式的静态调用。

Dim url As String = "https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg"

Dim sw As new System.Diagnostics.stopwatch()
sw.Start()

' Lets see how code can do without regex
Dim joined As String

For t as Integer = 1 to 5000
    Dim pos as Integer

    Dim results() As String = url.Split("/".ToCharArray(), StringSplitOptions.None)
    For i As Integer = 0 To results.Length - 1
        If results(i).Length > 1 AndAlso results(i).StartsWith("s", StringComparison.OrdinalIgnoreCase) Then
            pos = results(i).IndexOf("-", 1)
            If pos > 1 Then ' we care only of "s+[0-9]"
                results(i) = results(i).Replace(results(i).Substring(0, pos), "/s1600-")
            End If
        End If
    Next
    joined = String.Join("/",  results, 0, results.Length)
Next

sw.Stop()
Console.WriteLine("Non-Regex: " & sw.ElapsedMilliseconds & " ms. Output: " & joined)

' New Test  
sw.reset()  

' Lets see how nice shared regex call really is
Dim output As String
sw.Start()
For t As Integer = 1 to 5000
    output = Regex.Replace(url, "\/s\d+-", "/s1600-")
Next

sw.Stop()

Console.WriteLine("Regex Static: " & sw.ElapsedMilliseconds & " ms. Output: " & output)

sw.Reset()
Dim output2 As String
sw.Start()

Dim rx As New Regex("\/s\d+-")
For t As Integer = 1 To 5000
    output2 = rx.Replace(url, "/s1600-")
Next

sw.Stop()

Console.WriteLine("Regex Instance: " & sw.ElapsedMilliseconds & " ms. Output: " & output2)

结果样本:

  

非正则表达:14毫秒   正则表达式静态:15毫秒   正则表达式实例:9毫秒。

  非正则表达式:13毫秒   正则表达式静态:14毫秒   正则表达式实例:8毫秒。

  非正则表达式:13毫秒   正则表达式静态:14毫秒   正则表达式实例:8毫秒。

  非正则表达式:15毫秒   正则表达式静态:14毫秒   正则表达式实例:8毫秒。

  非正则表达式:13毫秒   正则表达式静态:16毫秒   正则表达式实例:8毫秒。

我看到的是调用Regex实例方法更好。随着url越来越长,非正则表达式方法的表现会更差。但是,同样适用于正则表达式。我用更多的部件制作了更长的网址,并开始

  

非正则表达:33毫秒   正则表达式静态:26毫秒   正则表达式实例:21毫秒。

所以,让我们说,如果你的网址会变长但路径部分的数量会保持不变,那么正则表达式会失去一些性能,而非正则表达式方法也会失去一些,但不会那么多。但是如果你添加零件,非正则表达式真的会恶化。

您可以根据具体用途优化代码。不要以为使用一种方法而不是另一种方法总是正确的。