I'm currently working with a piece of code in c#/Asp.net.
It uses a regex replace function structured like this:
return regex.Replace(pattern, match =>
{
// do something
return something;
});
An example pattern might be "This is a {sample} bit of text where the items in the brackets are {replaced}"
This form of replace syntax seems to be all or nothing — it gets all matches and will replace all matches with something.
Here's the full code which was posted by Mark Gravell:
How can I create a more user-friendly string.format syntax?
However, I need to be able to conditionally replace — get matches, iterate through them, and decide to replace or not.
Example
Given the input string:
"You are {age} years old and your first name is {firstName} and yourlast name is {lastName}.
Usage
string s = Format("You are {age} years old and your first name is {firstName} and yourlast name is {lastName}.", new {age = 18, firstName = "Foo"});
Current output
"You are 18 years old and your first name is Foo and yourlast name is ."
Desired output
"You are 18 years old and your first name is Foo and yourlast name is {lastName}."
Any advice appreciated.