在多个但特定位置替换字符串中的文本

时间:2017-05-01 15:34:07

标签: vb.net

我正在试图找出如何替换特定位置的图像标记中的src属性。

我有这个来自数据库的字符串。

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"  />

src属性都是一样的, src =“(imageTag)”我需要用真实的东西替换......像这样:

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" />

我使用了String.Replace函数,如下所示:

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

但由于字符串包含多个src =“(imageTag)”

的实例,因此不会替换正确位置的文本

vb.net中是否有任何可以执行此操作的功能?

2 个答案:

答案 0 :(得分:4)

对于任何复杂的事情,我建议使用真正的HTML解析器,例如HTML Agility Pack。但是,如果输入字符串非常一致,那么使用正则表达式进行替换并不是一个糟糕的选择。不过,我提醒你,一旦你开始需要支持HTML格式的不同变化,我就会抛弃正则表达式方法并使用更多功能的东西。

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

在此处查看demo。正则表达式是一个非常强大的搜索和替换工具。一旦你擅长它,它也是一个非常强大的编码工具,因为它支持Visual Studio的查找/替换功能以及大多数其他开发人员应用程序。这是正则表达式id="([^"]*)" src="\(imageTag\)"的含义。

  • ([^"]*) - 创建一个捕获组(在替换模式中称为$1
    • ( - 开始捕获小组
    • [^"]* - 将所有字符与下一个"匹配。 ([^"]声明一个字符类,其中包含非"的所有字符,*表示“连续排列零个或多个”
    • ) - 结束捕获组

答案 1 :(得分: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

输出:

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"  />