简体   繁体   English

按顺序替换占位符

[英]Replace placeholders in order

I have a part of a URL like this: 我有一部分网址是这样的:

/home/{value1}/something/{anotherValue}

Now i want to replace all between the brackets with values from a string-array. 现在我想用字符串数组中的值替换括号之间的所有内容。

I tried this RegEx pattern: \\{[a-zA-Z_]\\} but it doesn't work. 我尝试了以下RegEx模式: \\{[a-zA-Z_]\\}但是它不起作用。

Later (in C#) I want to replace the first match with the first value of the array, second with the second. 稍后(在C#中),我想将第一个匹配项替换为数组的第一个值,将第二个替换为第二个。

Update: The /'s cant be used to separate. 更新:/不能用于分隔。 Only the placeholders {...} should be replaced. 仅占位符{...}应该被替换。

Example: /home/before{value1}/and/{anotherValue} 示例:/ home /之前{value1} /和// {anotherValue}

String array: {"Tag", "1"} 字符串数组:{“ Tag”,“ 1”}

Result: /home/beforeTag/and/1 结果:/ home / beforeTag / and / 1

I hoped it could works like this: 我希望它可以这样工作:

string input = @"/home/before{value1}/and/{anotherValue}";
string pattern = @"\{[a-zA-Z_]\}";
string[] values = {"Tag", "1"};

MatchCollection mc = Regex.Match(input, pattern);        
for(int i, ...)
{
    mc.Replace(values[i];
}        
string result = mc.GetResult;

Edit: Thank you Devendra D. Chavan and ipr101, 编辑:谢谢Devendra D. Chavan和ipr101,

both solutions are greate! 两种解决方案都很棒!

You can try this code fragment, 您可以尝试以下代码片段,

// Begin with '{' followed by any number of word like characters and then end with '}'
var pattern = @"{\w*}"; 
var regex = new Regex(pattern);

var replacementArray = new [] {"abc", "cde", "def"};
var sourceString = @"/home/{value1}/something/{anotherValue}";

var matchCollection = regex.Matches(sourceString);
for (int i = 0; i < matchCollection.Count && i < replacementArray.Length; i++)
{
    sourceString = sourceString.Replace(matchCollection[i].Value, replacementArray[i]);
}

[a-zA-Z_] describes a character class. [a-zA-Z_]描述一个字符类。 For words, you'll have to add * at the end (any number of characters within a-zA-Z_ . 对于单词,您必须在末尾添加*a-zA-Z_任意数量的字符。

Then, to have 'value1' captured, you'll need to add number support : [a-zA-Z0-9_]* , which can be summarized with: \\w* 然后,要捕获'value1',您需要添加数字支持: [a-zA-Z0-9_]* ,可以将其概括为: \\w*

So try this one : {\\w*} 因此,请尝试以下操作: {\\w*}

But for replacing in C#, string.Split('/') might be easier as Fredrik proposed. 但是对于用C#替换,如Fredrik所建议的那样,string.Split('/')可能会更容易。 Have a look at this too 也看看这个

You could use a delegate, something like this - 您可以使用一个代表,像这样-

string[] strings = {"dog", "cat"};
int counter = -1;
string input = @"/home/{value1}/something/{anotherValue}";
Regex reg = new Regex(@"\{([a-zA-Z0-9]*)\}");
string result = reg.Replace(input, delegate(Match m) {
    counter++;
    return "{" + strings[counter] + "}";
});

My two cents: 我的两分钱:

// input string     
string txt = "/home/{value1}/something/{anotherValue}";

// template replacements
string[] str_array = { "one", "two" };

// regex to match a template
Regex regex = new Regex("{[^}]*}");

// replace the first template occurrence for each element in array
foreach (string s in str_array)
{
    txt = regex.Replace(txt, s, 1);
}

Console.Write(txt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM