用Regex.Replace c#替换双引号之间的字符串

时间:2014-12-22 17:29:38

标签: c#

我想替换这一行: 设置项目“dog1.abc”

这一行:

设置项目“dog3.abc”

使用Regex.Replace。 一些帮助,任何人???

2 个答案:

答案 0 :(得分:1)

您可以使用此代码:

        string input = "set project " + "dog1.abc";
        string pattern = "dog..abc";
        string replacement = "dog3.abc";
        string result = Regex.Replace(input,pattern ,replacement );
        Console.WriteLine("Original String: {0}", input);
        Console.WriteLine("Replacement String: {0}", result);

答案 1 :(得分:-1)

放手一搏:

string input = "set project \"dog1.abc\"";
string replacement = "\"dog3.abc\"";
// Replace any quoted string with the specified replacement text.
string result = Regex.Replace(input, "\".*\"", replacement);

// Note that the longest pattern match is replaced, aka "greedy" matching.
input = "pair moose \"Bullwinkle\" and squirrel \"Rocky\"";
result = Regex.Replace(input, "\".*\"", replacement);