I need to write a string replace function with custom wildcards support. I also should be able to escape these wildcards. I currently have a wildcard class with Usage, Value and Escape properties.

So let's say I have a global list called **Wildcards**. Wildcards has only one member added here:

```
Wildcards.Add(new Wildcard
{
    Usage = @"\Break",
    Value = Enviorement.NewLine,
    Escape = @"\\Break"
});

```

So I need a CustomReplace method to do the trick. I should replace the specified parameter in a given string with another one just like the `string.Replace`. The only difference here that it must use my custom wildcards.

```
string test = CustomReplace("Hi there! What's up?", "! ", "!\\Break");
// Value of the test variable should be: "Hi there!\r\nWhat's up?"
// Because \Break is specified in a custom wildcard in Wildcards

// But if I use the value of the wildcard's Escape member,
// it should be replaced with the value of Usage member.
test = CustomReplace("Hi there! What's up?", "! ", "!\\\\Break");
// Value of the test variable should be: "Hi there!\\BreakWhat's up?"

```

My current method doesn't support escape strings.
It also can't be good when it comes to performance since I call string.Replace two times and each one searches the whole string, I guess.

```
// My current method. Has no support for escape strings.
CustomReplace(string text, string oldValue, string newValue)
{
    string done = text.Replace(oldValue, newValue);
    foreach (Wildcard wildcard in Wildcards)
    {
        // Doing this:
        // done = done.Replace(wildcard.Escape, wildcard.Usage);
        // ...would cause trouble when Escape contains Usage.

        done = done.Replace(wildcard.Usage, wildcard.Value);
    }

    return done;
}

```

So, do I have to write a replace method which searches the string char by char with the logic to find and seperate both Usage and Escape values, then replace Escape with Usage while replacing Usage with another given string?

Or do you know an already written one?

Can I use regular expressions in this scenerio?

If I can, how? (Have no experience in this, a pattern would be nice)

If I do, would it be faster or slower than char by char searching?

Sorry for the long post, I tried to keep it clear and sorry for any typos and such; it's not my primary language. Thanks in advance.