0

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.

oguz ismail
  • 1
  • 16
  • 47
  • 69
John Ohara
  • 2,821
  • 3
  • 28
  • 54
  • 4
    Please make your question concrete: what text have you got and what do you need to get in the end? The snippet above looks like a sample code from the docs, what have *you* tried? What is your *problem*? – Wiktor Stribiżew Nov 06 '17 at 10:29
  • @WiktorStribiżew - I recently posted the question in a lot more detail and got slayed for a lack of clarity. I am writing an extension that will replace the properties in the brackets with properties I supply - like string.format. The above code will replace all and there is no option to opt out. I wan't to be able to find matches then choose to replace. I don't need extensive code, just an idea as to how to separate out the replace and match functionality. – John Ohara Nov 06 '17 at 10:39
  • In the code I linked to, just use `var pattern = @"(?<={).*?(?=})";` for it work with no other changes in the further code. Certainly, you may enhance it by using `@"{(.*?)}"` or `@"{([^{}]*)}"` pattern and then using `m => $"{{{variablesDictionary[m.Groups[1].Value]}}}"`. – Wiktor Stribiżew Nov 06 '17 at 10:40
  • Also, see [this demo](https://ideone.com/Bl17a0). – Wiktor Stribiżew Nov 06 '17 at 10:45
  • @WiktorStribiżew - I used the pattern but got nothing back - just empty brackets for everything. I've added a link to the piece of code I'm trying to make a change to which will help explain what I'm trying to achieve. – John Ohara Nov 06 '17 at 10:51
  • John, right now, we all understand what you are trying to do, but it is not at all clear what is wrong with *your* code. Once you specify the exact problem, I will be glad to reopen/answer your question. – Wiktor Stribiżew Nov 06 '17 at 11:01
  • @WiktorStribiżew - thanks for your help - the current code replaces all matching properties but at the same time removes any brackets that are not matched. What I need is to replace all the the matched properties, but leave the unmatched properties as they were. – John Ohara Nov 06 '17 at 11:05
  • John, I shared a demo link. It has `m => dct.ContainsKey(m.Groups[1].Value) ? $"{{{dct[m.Groups[1].Value]}}}" : m.Value` part: if the value in between `{...}` is known, if it is present in the dictionary, it is replaced. Else, `m.Value`, the whole match, is pasted back. – Wiktor Stribiżew Nov 06 '17 at 11:12
  • 1
    @WiktorStribiżew - using the match.value worked - thanks for that. – John Ohara Nov 06 '17 at 11:20

0 Answers0