2012-03-06 90 views
1

我需要编写带自定义通配符支持的字符串替换函数。我也应该能够逃脱这些通配符。我目前有一个带有Usage,Value和Escape属性的通配符类。支持自定义通配符并在C#中转义这些通配符的字符串替换函数

那么,假设我有一个全局列表通配符。通配符只有一个成员在这里添加:

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

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

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的时候,它在性能方面也不会很好。我猜,两次更换并且每个都搜索整个字符串。

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

所以,我必须写一个替代方法,该方法通过字符搜索字符串字符用逻辑来查找和单独的使用情况与逃生值,然后使用替代逃生而用另一个给定的字符串替换使用?

或者你知道一个已经写好的吗?

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

如果我可以,怎么样? (有没有这方面的经验,一个模式会很好)

如果我这样做,它会比字符搜索更快或更慢?

对不起,对于这篇较长的文章,我尽量保持清楚,并对任何错别字等都感到抱歉;这不是我的主要语言。提前致谢。

+5

你想在自定义函数中使用正则表达式,你可以通过从头开始使用正则表达式来避免使用正则表达式?这不是破坏了目标吗? – musefan 2012-03-06 13:02:50

+0

我不懂正则表达式。我的目标是在替换函数中使用自定义通配符,同时能够转义它们。如果正则表达式是一种方式,或者是一种比替代方式更好的方法,那么我会使用它。我的问题是“最佳方式是什么?”如果它是正则表达式“如何使用正则表达式来做到这一点?”因为我没有使用正则表达式的经验。 – 2012-03-06 14:08:17

回答

1

你可以试试这个:

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

我觉得这是最简单,最快的解决方案对于所有通配符只有一个Replace方法调用。

+0

简单而完全支持我的自定义通配符,包括自定义转义。谢谢! – 2012-03-07 10:18:42

1

所以,如果你很乐意使用正则表达式来满足你的需求,那么你应该check out this link。它有一些在.Net中使用的很好的信息。该网站还有许多关于谁来为许多不同需求构建正则表达式模式的例子。

对使用​​通配符的字符串替换可能是这样一个基本的例子...

string input = "my first regex replace"; 

string result = System.Text.RegularExpressions.Regex.Replace(input, "rep...e", "result"); 

//result is now "my first regex result" 

通知在Replace函数的第二个参数是怎样将一个正则表达式的字符串。在这种情况下,这些点充当通配符,它​​们基本上意味着“匹配任何单个字符”

希望这可以帮助您获得所需的内容。

+0

感谢您的链接。看起来这是正则表达式的最终指南。我一定会在有空的时候检查一下。 – 2012-03-07 10:16:06

0

如果您为通配符和转义方法定义了模式,则可以创建一个正则表达式,它将查找文本中的所有通配符。然后您可以使用MatchEvaluator来替换它们。

class Program 
{ 
    static Dictionary<string, string> replacements = new Dictionary<string, string>(); 

    static void Main(string[] args) 
    { 
     replacements.Add("\\Break", Environment.NewLine); 

     string template = @"This is an \\Break escaped newline and this should \Break contain a newline."; 

     // (?<=($|[^\\])(\\\\){0,}) will handle double escaped items 
     string outcome = Regex.Replace(template, @"(?<=($|[^\\])(\\\\){0,})\\\w+\b", ReplaceMethod); 

    } 

    public static string ReplaceMethod(Match m) 
    { 
     string replacement = null; 
     if (replacements.TryGetValue(m.Value, out replacement)) 
     { 
      return replacement; 
     } 
     else 
     { 
      //return string.Empty? 
      //throw new FormatException()? 
      return m.Value; 
     } 
    } 
} 
+0

一个很好的解决方案,谢谢,但它不支持自定义转义,我需要该功能。 – 2012-03-07 10:18:30

+0

除非您标准化,否则您需要编写自定义转义正则表达式,而不是每个项目的文本并执行多次。 – jessehouwing 2012-03-07 17:01:42

+0

您能否提供搜索模式,替换和自定义转义的样本(不仅仅是这一个)? – jessehouwing 2012-03-07 17:02:30