I need to substitute all marked strings in a given string with some value, like:
"This is the level $levelnumber of the block $blocknumber."
I want to translate that to:
"This is the level 4 of the block 2."
This is just an example. In real I have several $data tags in text that needs to be changed at runtime to some data. I don´t know what $data tags will come in string.
I´m planning to use Regex for it, but regex is really confusing.
I´ve tried with no success this (with several variants of double quotes, no quote, etc.):
public static ShowTags (string Expression)
{
var tags = Regex.Matches(Expression, @"(\$([\w\d]+)[&$]*");
foreach (var item in tags)
Console.WriteLine("Tag: " + item);
}
Any help appreciated.
[EDIT]
Working code:
public static ReplaceTagWithData(string Expression)
{
string modifiedExpression;
var tags = Regex.Matches(Expression, @"(\$[\w\d]+)[&$]*");
foreach (string tag in tags)
{
/// Remove the '$'
string tagname = pdata.Remove(0, 1);
/// Change with current value
modifiedExpression.Replace(pdata, new Random().ToString()); //Random simulate current value
}
return modifiedExpression;
}