user342706
user342706

Reputation:

Regex Replace with forward slash

Can someone please explain how to get this regex working? I'm trying to take this string:

"Test0/1"

and turn it into:

"Test0\/1"

I'm using this, but it is not working:

var test = Regex.Replace("Test0/1", @"/", @"\/");

It keeps giving me

"Test0\\/1"

Then I want to take the results of the string and put it into a Regex statement like so:

var match  = new Regex(test).Match(myString);

So the string 'test' has to be a valid regex statement.

Basically what I'm trying to do is take a list of interfaces off a device, create a regex statement out of them and then use that regex to compare results for other things in my code. Because of the way interfaces are formatted "FastEthernet0/1" for example, it is causing my regex to fail because you have to escape all forward slashes. I have to build this regex on the fly though because every device will have a different set of interfaces.

Upvotes: 1

Views: 7017

Answers (1)

Ryan Gates
Ryan Gates

Reputation: 4539

This is a function of Visual Studio automatically escaping the \ on your behalf. Look at the following question: What's the use/meaning of the @ character in variable names in C#?. Removing the @ symbol from @"\" turns the string into "\\".

Upvotes: 1

Related Questions