所以我有一些字符串,我想更換支架的一些事件中。使用正則表達式查找封裝在[]項目,然後替換它們
現在,這是我到目前爲止已經完成。和它的作品
string answerText = "This is an [example] string";
Match match = Regex.Match(answerText, @"\[(.*?)\]");
if(match.Success)
{
if(match.Value.Equals("[example]"))
{
answerText = answerText.Replace(match.Value, "awsome");
}
}
我試圖找出是如何做到這一點,如果答案文字看起來像這樣
string answerText = "This is an [example1] [example2] [example3] string";
更普遍的是,你可能想看看平衡的正則表達式,例如http://stackoverflow.com/questions/4284827/regular-expression-that-uses-balancing-groups –
當然,你不只是尋找[Regex.Replace](http://msdn.microsoft.com/en -us/library/xwewhkd1.aspx)方法? *在指定的輸入字符串內,用指定的替換字符串替換所有匹配正則表達式模式的字符串。* –
是否會有嵌套? – NullUserException