使用regex和vb.net替换String的一部分

时间:2016-05-29 10:29:15

标签: regex vb.net

我的应用程序将userdata.dll的路径存储在String中。

我需要转换此字符串:C:\Applications\User\userdata.dll

进入:C:\\Applications\\User\\userdata.dll

所有\都需要重复,与路径\的数量无关。

类似的东西:

Dim defaultPath As String = "C:\Applications\User\userdata.dll"

' Regex
Dim r As Regex = New Regex( ... )

' This is the replacement string
Dim Replacement As String = " ... $1 ... "

' Replace the matched text in the InputText using the replacement pattern
Dim modifiedPath As String = r.Replace(defaultPath,Replacement)

对此有何帮助?我试图关注这个问题:

How to replace some part of this string with vb.net?

但是无法找到如何制作这个正则表达式......

1 个答案:

答案 0 :(得分:2)

您可以使用

Dim pattern As String =  "\\"

Dim rgx As New Regex(pattern)

Dim input As String = "C:\Applications\User\userdata.dll"

Dim result As String = rgx.Replace(input, "\\")

Console.WriteLine(result) 

<强> Ideone Demo

如果您的意思是将\替换为\\,则可以使用

Dim pattern As String =  "\\+"

Dim rgx As New Regex(pattern)

Dim input As String = "C:\\\\Applications\User\userdata.dll"

Dim result As String = rgx.Replace(input, "\\")

<强> Ideone Demo