Simonetos Simonetos - 6 months ago 11
Vb.net Question

VB.NET - Find and replace a range of text from a text box

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.

Answer

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