繁体   English   中英

使用正则表达式和 vb.net 替换字符串的一部分

[英]Replace part of String, using regex and vb.net

我的应用程序将 userdata.dll 的路径存储在一个字符串中。

我需要转换这个字符串: 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)

这有什么帮助吗? 我试图遵循这个问题:

如何用 vb.net 替换此字符串的某些部分?

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

您可以使用

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 演示

如果您的意思是说将任意数量的\\替换为\\\\ ,那么您可以使用

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 演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM