字符串替换功能,支持自定义通配符,并在C#中转义这些通配符 [英] A string replace function with support of custom wildcards and escaping these wildcards in C#

查看:461
本文介绍了字符串替换功能,支持自定义通配符,并在C#中转义这些通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用自定义通配符支持字符串替换功能。我也应该能够逃脱这些通配符。我现在有一个带有Usage,Value和Escape属性的通配符类。

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"
});

所以我需要一个CustomReplace方法来做这个伎俩。我应该使用另一个字符串替换给定字符串中的指定参数,就像 string.Replace 一样。唯一的区别在于它必须使用我的自定义通配符。

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?"

我目前的方法不支持转义字符串。
当我调用string.Replace两次,并且每个搜索整个字符串时,我也不能很好,我猜。$ /

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;
}

所以,我必须编写一个替换方法,它通过char与逻辑找到并分离使用和Escape值,然后替换Escape与用法替换使用与另一个给定的字符串?

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?

我可以在这个scenerio中使用正则表达式吗?

Can I use regular expressions in this scenerio?

如果可以,怎么办? (没有任何经验,一个模式会很好)

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

如果我这样做,它会比char通过char搜索更快或更慢吗?

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.

推荐答案

你可以试试这个:

public string CustomReplace(string text, string oldValue, string newValue)
{
    string done = text.Replace(oldValue, newValue);

    var builder = new StringBuilder();
    foreach (var wildcard in Wildcards)
    {
        builder.AppendFormat("({0}|{1})|", Regex.Escape(wildcard.Usage),
            Regex.Escape(wildcard.Escape));
    }
    builder.Length = builder.Length - 1; // Remove the last '|' character

    return Regex.Replace(done, builder.ToString(), WildcardEvaluator);
}

private string WildcardEvaluator(Match match)
{
    var wildcard = Wildcards.Find(w => w.Usage == match.Value);

    if (wildcard != null)
        return wildcard.Value;
    else
        return match.Value;
}

我认为这是最简单和最快的解决方案,因为只有一个替换所有通配符的方法调用。

I think this is the easiest and fastest solution as there is only one Replace method call for all wildcards.

这篇关于字符串替换功能,支持自定义通配符,并在C#中转义这些通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆