2013-01-06 39 views
0

我想在C#中使用Capture函數捕獲值時遇到問題。 我的代碼在字符串中查找很多模式,所以我使用匹配集合,然後對於每個匹配我使用Capture函數。但是當我想替換captureOut.value時,它不起作用。如何從C#中的捕獲函數捕獲值

我的代碼:

MatchCollection matches = Regex.Matches(string, @"\d*\.*\d+\s") 

foreach (Match matchOut in matches) 
{ 
    foreach (Capture captureOut in matchOut.Captures) 
    Match match1 = Regex.Match(captureOut.Value, @"\d*\.*\d+"); 
::::: //} 
    output = Regex.Replace(output,captureOut.Value, Function1); 
} 
// i change the value of pattern based on the output of function 1 

我的這部分代碼,我不知道爲什麼capture out.value不起作用。

+3

你能重新澄清你想要做什麼嗎?也許提供一些輸入字符串的例子以及你想要的結果是什麼? – JLRishe

回答

1

只有在正則表達式中有組時,即使用()中包含的正則表達式部分,使用捕獲屬性纔有意義。 由於你的正則表達式沒有,只有一個捕獲組,它是與正則表達式匹配的整個字符串。