1

I'm trying to figure out how to replace the src property in an image tag at a specific location.

I have this string that is coming from the database.

The library will be closed today.
<img id="fjs48b94-1" src="(imageTag)" alt="png"  />
<br />
Please use this online tool to check-in and check-out material.
<img id="fjs44213-4" src="(imageTag)" alt="gif"  />
<br />Contact Us for any questions.
<img id="fjs22643-9" src="(imageTag)" alt="jpg"  />

The src properties are all the same, src="(imageTag)" I need to replace with something real...like this:

The library will be closed today.
<img id="fjs48b94-1" src="http://fsu-libs.org/images/fjs48b94-1.png" />
<br />
Please use this online tool to check-in and check-out material.
<img id="fjs44213-4" src="http://fsu-libs.org/images/fjs44213-4.gif" />
<br />Contact Us for any questions.
<img id="fjs22643-9" src="http://fsu-libs.org/images/fjs22643-9.jpg" />

I played around with the String.Replace function like this:

Dim newText = originalString.Replace("src=""(imageTag)""", "src="http://fsu-libs.org/images/fjs22643-9.jpg"")

but that won't replace the text at the proper location since the string contains multiple instances of src="(imageTag)"

Is there any function in vb.net that can do this?

djv
  • 15,168
  • 7
  • 48
  • 72
SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • I assume that you're trying to rewrite a specific img tag src property. Using JQuery would solve this problem. The link below details several methods: http://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery This would run on the client side though, not in the code behind. – Prescott Chartier May 01 '17 at 15:50

2 Answers2

4

For anything complicated, I would recommend using a true HTML parser, such as HTML Agility Pack. However, if the input string is very consistent, then using regex to do the replacement isn't a terrible option. I caution you, though, as soon as you start needing to support different variations in the HTML formatting, I would ditch the regex method and go with something more versatile.

output = Regex.Replace(input, "id=""([^""]*)"" src=""\(imageTag\)""", "id=$1 src=""http://fsu-libs.org/images/$1""")

See a demo here. Regex is a very powerful tool for doing searches as well as replacements. Once you get good at it, it's also a really powerful tool for coding, since it's supported in Visual Studio's Find/Replace feature as well as most other developer apps. Here's the meaning of the regex pattern id="([^"]*)" src="\(imageTag\)".

  • ([^"]*) - creates a capturing group (referred to in the replacement pattern as $1.
    • ( - Begin a capturing group
    • [^"]* - Match all characters up to the next ". ([^"] declares a character class which includes all characters that are not " and the * means "zero or more of them in a row")
    • ) - End the capturing group
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • thanks, that looks promising. So would the 'input' be any string containing the image tags? How does the regex "know" how to match up the correct id with the correct src attribute? thanks! – SkyeBoniwell May 01 '17 at 17:25
  • Yes. The input could contain the full text containing multiple image tags. The regex finds and replaces each image tag as a single sub string. The pattern will only match a substring that has that exact format, with an id attribute followed by an arc attribute, so each match is a complete pair and it replaces it accordingly. – Steven Doggart May 01 '17 at 17:31
1
Module Module1
 Sub Main()
     Dim input As String = My.Computer.FileSystem.ReadAllText("C:\1.txt")
     Dim fillData As New List(Of String) From {"http://www.Loremipsum.com/123.jpg", "http://www.Loremipsum.com/456.jpg", "http://www.Loremipsum.com/789.jpg"}
     Dim output As String = populate(input, "(imageTag)", fillData)
     Console.WriteLine(String.Format("Input: {0}{1}{0}", vbNewLine, input))
     Console.WriteLine(String.Format("Output: {0}{1}{0}", vbNewLine, output))
     Console.ReadLine()
 End Sub

 Private Function populate(input As String, toPopulate As String, data As List(Of String))
     Dim sr As New IO.StringReader(input)
     Dim x As Integer = 0
     Dim output As String = String.Empty
     While sr.Peek <> -1
         Dim line As String = sr.ReadLine
         If line.Contains(toPopulate) Then
             line = line.Replace(toPopulate, data.Item(x))
             x += 1
         End If
         output += line & vbNewLine
     End While
     Return output
 End Function
End Module

Output:

Input:
The library will be closed today.
<img id="fjs48b94-1" src="(imageTag)" alt="png"  />
<br />
Please use this online tool to check-in and check-out material.
<img id="fjs44213-4" src="(imageTag)" alt="gif"  />
<br />Contact Us for any questions.
<img id="fjs22643-9" src="(imageTag)" alt="jpg"  />

Output:
The library will be closed today.
<img id="fjs48b94-1" src="http://www.Loremipsum.com/123.jpg" alt="png"  />
<br />
Please use this online tool to check-in and check-out material.
<img id="fjs44213-4" src="http://www.Loremipsum.com/456.jpg" alt="gif"  />
<br />Contact Us for any questions.
<img id="fjs22643-9" src="http://www.Loremipsum.com/789.jpg" alt="jpg"  />
EuX0
  • 137
  • 8