替換佔位符我有這樣一個URL的一部分:爲了
/home/{value1}/something/{anotherValue}
現在我想從一個字符串數組值替換所有的括號內。
我試過這個RegEx模式:\{[a-zA-Z_]\}
但它不起作用。
後來(在C#中)我想用第一個數組的第一個值替換第一個匹配,第二個替換第二個。
更新:/不能用於分隔。只有佔位符{...}應該被替換。
示例:/家庭/前{VALUE1} /和/ {anotherValue}
字符串數組:{ 「標記」, 「1」}
結果:/家庭/ beforeTag /和/ 1
我希望它可以是這樣的:
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;
編輯: 謝謝德文德拉·查萬D.和ipr101,
兩種解決方案都是非常重要的!
爲什麼正則表達式?難道你不能只是將字符串拆分爲'/'並使用索引1和3? –
你有代碼示例和「之前」和「之後」字符串使問題更清晰嗎? – CodeCaster
使用該模式,在你的例子中'{[a-zA-Z0-1] *}'或'{\ w *}'會給出想要的結果。 –