【问题标题】:Replace part of String, using regex and vb.net使用正则表达式和 vb.net 替换部分字符串
【发布时间】:2016-05-29 19:34:09
【问题描述】:

我的应用程序将 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)

对此有任何帮助吗?我正在尝试关注这个问题:

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

但不知道如何制作这个正则表达式...

【问题讨论】:

标签: regex vb.net


【解决方案1】:

你可以使用

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

【讨论】:

  • 不错的答案,但从技术上讲,您不需要字符类。您可以只使用 \\ 作为模式。
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多