Reputation: 6866
I have a the following path which I need to convert to URL.
string path = @"\\TestServer\User_Attachments$\Data\Reference\Input\Test.png";
I'm trying to replace the replace the special chars in this string. So
\\ will become
//with an
httpsadded at the front i.e.
https://`\
will become /
User_Attachments$
will become User_Attachments
The final string should look like
string url = "https://TestServer/User_Attachments/Data/Reference/Input/Test.png"
To achieve this I've come up with the following regex
string pattern = @"^(.{2})|(\\{1})|(\${1})";
I then match using the Matches()
method as:
var match = Regex.Matches(path, pattern);
My question is how can I check to see if the match is success and replace the appropriate value at the appropriate group and have a final url
string as I mentioned above.
Here is the link to the regex
Upvotes: 0
Views: 271
Reputation: 9519
As mentioned above, i'd go for a simple Replace
:
string path = @"\\TestServer\User_Attachments$\Data\Reference\Input\Test.png";
var url = path.Replace(@"\\", @"https://").Replace(@"\", @"/").Replace("$", string.Empty);
// note if you want to get rid of all special chars you would do the last bit differently
For example, taken out of one of these SO answers here: How do I remove all non alphanumeric characters from a string except dash?
// assume str contains the data with special chars
char[] arr = str.ToCharArray();
arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c)
|| char.IsWhiteSpace(c)
|| c == '-'
|| c == '_')));
str = new string(arr);
Upvotes: 3
Reputation: 126
You can do it like this
string path = @"\\TestServer\User_Attachments$\Data\Reference\Input\Test.png";
string actualUrl=path.Replace("\\","https://").Replace("\","/")
Upvotes: 0