Reputation: 978
Into a Visual Basic project I am trying to replace a part of URL coming from a text box. For example, I have this https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg
URL into TextBox1
and I want to replace the part /s912-
with /s1600-
.
I did it by doing something like this: Dim url = Replace(TextBox1.Text, "/s912-", "/s1600-")
. But, this URL isn't the same every time. The part /s912-
coould be /s800-
for example. So my next though is to use an asterik: Dim url = Replace(TextBox1.Text, "/s*-", "/s1600-")
. Of course it doesn'r work! So I need help with the syntax or a better idea.
Upvotes: 0
Views: 884
Reputation: 19350
Consider this experiment. I want to show you another method and why Regex
can be sometimes detrimental. Although, I am NOT saying that in your case you shouldn't use static call of 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)
Results samplings:
Non-Regex: 14 ms.
Regex Static: 15 ms.
Regex Instance: 9 ms.
Non-Regex: 13 ms.
Regex Static: 14 ms.
Regex Instance: 8 ms.
Non-Regex: 13 ms.
Regex Static: 14 ms.
Regex Instance: 8 ms.
Non-Regex: 15 ms.
Regex Static: 14 ms.
Regex Instance: 8 ms.
Non-Regex: 13 ms.
Regex Static: 16 ms.
Regex Instance: 8 ms.
What I see is calling Regex instance methods work better. Non-regex method will perform worse with url getting longer. But, same will go for regex. I made url longer with more parts, and started to get
Non-Regex: 33 ms.
Regex Static: 26 ms.
Regex Instance: 21 ms.
So, lets say, if your url will get longer but number of path parts will remain same, regex will lose some performance, and non-regex method will lose some too but not as much. But if you add parts, non-regex will really deteriorate.
It is in your hands to optimize your code for your specific usage. Do not assume that using one method over another is always right thing to do.
Upvotes: 1
Reputation: 8160
Regex.Replace
can be used to search/replace using a regular expression.
Dim input = "https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg"
Dim output = Regex.Replace(input, "/s\d+-", "/s1600-")
output
is:
https://lh3.googleusercontent.com/blahblahblah/s1600-Ic42/blahblahblah.jpg
Upvotes: 2
Reputation: 19350
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)
Result:
https://lh3.googleusercontent.com/blahblahblah/s1600-Ic42/blahblahblah.jpg
Upvotes: 1