2

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...

1

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer, but technically, you don't need the character class. You could just use \\ as the pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.