I have a url like
http://www.somesite.com/$(someKey).php
I have a dictionary includes these keys, what I need is using the Regex to replace the $(*) with the value in the dictionary labeled with that key.
How can I do that?
You may use the Regex.Replace Method. Try this:
class Program
{
static void Main(string[] args)
{
var dict = new Dictionary<string, string>();
dict.Add("someKey1", "MyPage1");
dict.Add("someKey2", "MyPage2");
var input = "http://www.somesite.com/$(someKey2).php";
var output = Regex.Replace(input, @"\$\((.*?)\)", m =>
{
return dict[m.Groups[1].Value];
});
}
}
\$\(.*?\) - otherwise the match for .* is greedy and there are going to be problems for cases with 2 or more possible matches.
Regex.Replace()method.