Lucas Matos
Lucas Matos

Reputation: 1162

Replace part of String, using regex and vb.net

My application stores the path of the userdata.dll in a String.

I need to convert this string: C:\Applications\User\userdata.dll

into this: C:\\Applications\\User\\userdata.dll

All \ will need to be duplicated, independent on how many \ the path have.

Something like:

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)

Any help on this? I am trying to follow this question:

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

But cant find out how to make this Regex...

Upvotes: 2

Views: 1480

Answers (1)

rock321987
rock321987

Reputation: 11032

You can use

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

If you mean to say that replace any number of \ to \\, then you can use

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

Upvotes: 2

Related Questions